问题
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