Java versions prior Java 8 requires native code to be in a shared library, but I've read that with Java 8 it's possible to use static linked libraries with JNI. I have searched for examples but couldn't find any.
How can I statically link a JNI library into my java application?
I was following a tutorial video on compilers on YouTube, when I came across the fact that the C Programming Language is faster because it allows both static linking and dynamic linking, but Java allows only dynamic linking and that is why C is much faster than Java.
Static Libraries: A Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.
Dynamic linking means that the code for some external routines is located and loaded when the program is first run. When you compile a program that uses shared libraries, the shared libraries are dynamically linked to your program by default.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With