The Java Native Interface (JNI) is a standard way in which any Java program can interact with native code. The process for working with native code can be summarized as follows:
Compile the Java class as normal.
Run javah with the class name, which generates a header stub.
Write the native C function and export it with the given function signature.
Compile the code into a dynamically linked library.
Load the library into the runtime with
System.loadLibrary
.Execute the native method as normal.
The name of the library is dependent on the operating system; some call the library name.dll
, some call it libname.so
, and others libname.dylib
. However, Java just uses the real portion of the library name; so, all three platforms use the same Java code, System.loadLibrary("name")
, to load the library.