Java Language Java Native Interface

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Parameters

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

Remarks

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?