Java code and Java Runtime
Java is a platform independent programming language. An application written in Java does not require recompilation for running in different platforms. Java compiler never compiles the code to native platform instructions binaries which is often seen with C, C++ programming languages. Java compiler (JAVAC) takes .java source files and compiles them into object files. These object files are in the form of JAVA byte code. To run these object codes JAVA setup provides JAVA runtime Environment (JRE). JAVA runtime Environment consists of a program running tool as JAVA and a couple of libraries and run time binaries. These are platform dependent. JAVA makes JRE for each platforms. JRE for Windows, Linux, Android are different kind of binaries and they are native to platfrom architectures like x86, x86_64, ARM, ARM64 etc. JAVA byte codes are interpreted by the runtime binary to translate to native instructions and then they run as native code.
Java Native Interface/JNIJAVA is good for writing codes without much knowledge of the platform on which it will be running. However system applications may need to access some of the system resources which are strictly platform dependent. C, C++ are the languages which can access OS resources and low level interfaces. Java cannot access this directly from java context. However there is a provision in Java to interface with native binaries. This interface is well known as Java Native Interface or JNI in short. JNI in the gateway between JAVA and native binaries, written in C/C++/VB or any other compiled languages. Most of the system applications are developed in two parts. One part which is tightly coupled to OS and low layer calls are written with C,C++. These binaries exposes native calls to JAVA environment. The other halves of the JAVA code calls these native interfaces.
Above code has three functions which are declared with native keyword. Native keyword informs Java Compiler that the function can be linked even if the definition of HelloWorld(), CallC() and SetMembers() do not exist. This is similar to importing a function from DLL/SO file. Java compiler puts these interfaces in import table or checks import table of DLL/so during runtime linking.
JNI library error
At this point if we try to execute this code we will get error as Java runtime will fail to load import library MyJNI.dll or libMyJNI.so.
>javac MyJNI.java >java MyJNI Unable to load MyJNI library
So the java side of the application is ready but the native implementation part is missing here. Let us see what are the things we need here.
- C/C++ header file contains the native function prototypes or declarations
- C/C++ source file contains definitions of the routines
- Native DLL binary which will be loaded and executed.
JAVAH & Interface generation
We need a tool called JAVAH which generates this interface. JAVAH comes with JDK and no additional installation or setup is required. Javah can be executed in the same way as we execute comiler javac or java. Javah takes byte code file name (without .class extension) as argument. It process than binary and generates a C/C++ header file in the same name.
JNI header file
Note carefully javah has generated three function prototype in C/C++. JNIEXPORT macro tells C/C++ compiler to export these functions from DLL/SO library so that java run time can import these calls. Additionally there are two extra arguments for each functions. First argument is of type JNIEnv which gives a pointer to Java Runtime Environment. JNIEnv provides many interfaces which native binary can utilize. Next argument is of type jobject which represents the object itself or the "this" pointer. We can access members of the object through this arguments. Next arguments are same as declared in java. jboolean, jbyte, jchar, jshort, jlong, jfloat, jdouble are type definition of unsigned char, signed char, signed short, short, int, long, float and double in C/C++. Now java treats strings as UNICODE thus jstring is not directly char * but conversions are possible in both the ways.
- Java_MyJNI_HelloWorld() is the equivalent JNI prototype of HelloWorld() in java.
- Java_MyJNI_CallC() is the equivalent JNI prototype of CallC() in java. It is calling all arguments by value.
- Java_MyJNI_SetMembers() is the equivalent JNI prototype of SetMembers() in java.
Implement Native C/C++ Code
Now we will implement these interfaces in a dynamic link library in Windows or in a shared library in Linux. We need to create a C/C++ source file and may need to copy this header file to the same folder. Here we created MyJNI.c in the same folder where MyJNI.h is located. We should include this file and implement the definitions.
jchar, jshort, jlong, jfloat, jdouble are type definition of char, short, long, float and double in C/C++ thus we can used these directly. But this is not true for the strings. Java treats strings char char as UNICODE. There are two bytes for each characters. First character contains ASCII value and next one contains language id. jstring can be converted to UTF char* and vice versa. GetStringUTFChars() can be used to extract char* from Java strings. NewStringUTF() is used to allocate a new UNICODE string from UTF char*. ReleaseStringUTFChars() is used to release existing buffer.
Here in Java_MyJNI_CallC(), we are simply printing the values passed from Java in C/C++ context and directly returning to Java context. Java_MyJNI_SetMembers() is not taking arguments. Every C++/Java function has inherent member called this pointer. This function uses this object to access all member variables. JNI can access and modify all the members. These are the steps to access/modify Java members.
- GetObjectClass() returns the class reference
- GetFieldID() returns field ID of the member from class reference
- GetXXXXField()/GetObjectField returns an instance of the member variable from object
- SetXXXXField()/SetObjectField can be used to set the member value
JNI Block Diagram
Now we need to place this native library in the same folder where our Java Class binary located and can run. If we have more than one class We can also place this in the Java Runtime binary folder jre\bin if we want to access this from more than one java class located in different folder.
:>java MyJNI Hello World! Calling by Value Arguments from Java z (boolean) = true b (byte) = J c (char) = K s (short) = 10 i (int) = 100 l (long) = 1000 f (float) = 1.200000 d (double) = 1.500000 str (string) = Hello From Java! Calling by Reference Java members (from C) z (boolean) = true b (byte) = C c (char) = D s (short) = 20 i (int) = 200 l (long) = 2000 f (float) = 2.200000 d (double) = 2.500000 str (string) = Hello from Java! End of Calling by Reference Java Members (changed) z (boolean) = false b (byte) = D c (char) = E s (short) = 10 i (int) = 100 l (long) = 1000 f (float) = 1.2 d (double) = 1.5 str (string) = Hello form C!
About our authors: Team EQA
You have viewed 1 page out of 27. Your DLL learning is 0.00% complete. Login to check your learning progress.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Draw on printer Memory leaks