Linking static library with JNI
The Java 8 enhancement https://openjdk.java.net/jeps/178 is meant for the JVM.
Given two files:
- Main.java
- Main.c
Create libnative.so:
javac Main.java
javah Main
gcc -c Main.c
gcc -c Main.c -I /home/dx/.sdkman/candidates/java/current/include/linux -I /home/dx/.sdkman/candidates/java/current/include
gcc -shared -o libnative.so Main.o
Create libnative.a:
ar -cvq libnative.a Main.o
For each libnative.a, libnative.so test run via:
java -Djava.library.path=. Main
Result:
- Success execution when
libnative.so
- Fail execution when
libnative.a
This proves that 178 is for the JVM.
References:
- https://github.com/java-native-access/jna/issues/387
- https://github.com/typelogic/jni
- https://asciinema.org/a/HTPWvr0WLyEL6iz3ahGUG1A63
Accoding to the JEP 178 you linked to in your comment, you don't have to do anything differently. System.loadLibrary
will now load both dynamic and static libraries.
Require no changes to existing Java code in order to use a static native library as opposed to a dynamic native library. A method invocation of the form System.loadLibrary("foo"), in particular, should be able to load the "foo" library regardless of whether that library is provided in static or dynamic form.
You probably just need to make sure your java.library.path is set correctly.
The Java SE 8 specification has been changed to support static linking, and static linking is implemented in the JDK. This is mentioned briefly in the spec for System.loadLibrary. The sections of the JNI Specification to which it refers are here and here.
Native method signatures and data types are the same for statically and dynamically linked methods. You might have to hack on the JDK makefiles to get it to link your library statically, though.
One significant difference is the way static libraries are initialized. Dynamic libraries are initialized by calling the JNI_OnLoad
function and are deinitialized by calling JNI_OnUnload
. Each dynamic library can have its own version of these functions. If there are multiple statically linked libraries, clearly they can't all have functions with these same names. For a static library named libname
the load/unload functions are JNI_OnLoad_libname
and JNI_OnUnload_libname
.
The JNI_OnLoad_libname
function must return a value of JNI_VERSION_1_8
or higher. If it doesn't, the JVM will ignore the static library.
Basically, if you call System.loadLibrary("foo")
, the system looks for the function JNI_OnLoad_foo
in the running executable image, and if it's found, it assumes that the library is statically linked, and its native methods are searched for within the running image. If JNI_OnLoad_foo
is not found, then the usual searching and loading of dynamic libraries takes place, and native methods are linked from the dynamic library so found.