JNI - Cannot find native library

一个人想着一个人 提交于 2021-02-05 11:11:20

问题


I have a JNI wrapper for a C++ native library. When I try to load the library in my Java code, I see the following exception:

java.lang.UnsatisfiedLinkError: no Foo in java.library.path

I load the library like this:

System.loadLibrary("Foo");

I set java.library.path as follow:

java.library.path = /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/lib

I added -XshowSettings:properties to the JVM options and confirmed that the java.library.path is set as shown above.

Here's the library .so on the filesystem:

$ l /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/lib/libFoo.so 
-rwxr-xr-x  1 root  wheel   448K 29 Oct 14:23 /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/lib/libFoo.so

Environment information:

java.version = 1.7.0_40
os.arch = x86_64
os.name = Mac OS X
os.version = 10.10

So why can't I load the library?


回答1:


On Mac OS X, the JDK will not search the path for files ending in .so, or anything else than .dylib or .jnilib. We need to make sure filenames of native libraries end with .dylib or .jnilib.

Reference: https://developer.apple.com/library/mac/documentation/Java/Conceptual/Java14Development/05-CoreJavaAPIs/CoreJavaAPIs.html




回答2:


Place your .so files in jni/libs folder. If you have the library for multiple platforms, you need to create a folder for each platform and place the corresponding file. For example jni/libs/x86 will have the .so file built for x86 platforms. If you are using Android Studio, it is slightly different than this.



来源:https://stackoverflow.com/questions/26658072/jni-cannot-find-native-library

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