Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

java - Linking static library with JNI

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...