Linking static library with JNI

匿名 (未验证) 提交于 2019-12-03 02:49:01

问题:

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?

回答1:

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.



回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!