Java Language Java Native Interface

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


JNIEnvPointer to the JNI environment
jobjectThe object which invoked the non-static native method
jclassThe class which invoked the static native method


Setting up JNI requires both a Java and a native compiler. Depending on the IDE and OS, there is some setting up required. A guide for Eclipse can be found here. A full tutorial can be found here.

These are the steps for setting up the Java-C++ linkage on windows:

  • Compile the Java source files (.java) into classes (.class) using javac.
  • Create header (.h) files from the Java classes containing native methods using javah. These files "instruct" the native code which methods it is responsible for implementing.
  • Include the header files (#include) in the C++ source files (.cpp) implementing the native methods.
  • Compile the C++ source files and create a library (.dll). This library contains the native code implementation.
  • Specify the library path (-Djava.library.path) and load it in the Java source file (System.loadLibrary(...)).

Callbacks (Calling Java methods from native code) requires to specify a method descriptor. If the descriptor is incorrect, a runtime error occurs. Because of this, it is helpful to have the descriptors made for us, this can be done with javap -s.

Got any Java Language Question?