Java Native Interface

Download java eBook

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.

Related Examples

Stats

347 Contributors: 6
Sunday, October 16, 2016
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook