Search code examples
javajava-native-interface

JNI - Cannot find native library


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?


Solution

  • 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