Java Native Interface
suggest changeVersions
[{“Name”:“Java SE 1.1”,“GroupName”:null},{“Name”:“Java SE 1.2”,“GroupName”:null},{“Name”:“Java SE 1.3”,“GroupName”:null},{“Name”:“Java SE 1.4”,“GroupName”:null},{“Name”:“Java SE 5”,“GroupName”:null},{“Name”:“Java SE 6”,“GroupName”:null},{“Name”:“Java SE 7”,“GroupName”:null},{“Name”:“Java SE 8”,“GroupName”:null},{“Name”:“Java SE 9 (Early Access)”,“GroupName”:null}]
Parameters
Parameter | Details |
——— | —–– |
JNIEnv | Pointer to the JNI environment |
jobject | The object which invoked the non-static native method |
jclass | The 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) usingjavac. - Create header (
.h) files from the Java classes containingnativemethods usingjavah. 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 thenativemethods. - 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.