Java Library Path - UnsatisfiedLinkError on Mac

TheBlackBird picture TheBlackBird · May 28, 2018 · Viewed 10.1k times · Source

I know, this is not the first library path problem, but I really don't know what the problem is. I need a KernelWrapper library for my project, and including it on Linux is no problem at all. However, when I try to include it in Mac (over Terminal or directly in the IDE), I always get

Exception in thread "main" java.lang.UnsatisfiedLinkError: no KernelWrapper in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at cTools.KernelWrapper.<clinit>(KernelWrapper.java:6)
at shell.main(shell.java:18)

I printed out the library path using

System.out.println(System.getProperty("java.library.path"));

and it shows me that the path of the folder is correct and included. However, the KernelWrapper class somehow doesn't work properly. But it is the exact same folder which was included on Unix. What am I missing?

Answer

ARVINDER SINGH picture ARVINDER SINGH · May 28, 2018

The UnsatisfiedLinkError is thrown when an application attempts to load a native library like .so in Linux, .dll on Windows or .dylib in Mac(in your case) and that library does not exist. Specifically, in order to find the required native library, the JVM looks in both the PATH environment variable and the java.library.path system property.

First of all you must verify that the parameter passed in the System.loadLibrary method is correct and that the library actually exists. Notice that the extension of the library is not required. Thus, if your library is named SampleLibrary.dll, you must pass the SampleLibrary value as a parameter. Moreover, in case the library is already loaded by your application and the application tries to load it again, the UnsatisfiedLinkError will be thrown by the JVM. Also, you must verify that the native library is present either in the java.library.path or in the PATH environment library of your application. If the library still cannot be found, try to provide an absolute path to the System.loadLibrary method. In order to execute your application, use the -Djava.library.path argument, to explicitly specify the native library. For example, using the terminal (Linux or Mac) or the command prompt (Windows), execute your application by issuing the following command:

java -Djava.library.path= "<path_of_your_application>" –jar <ApplicationJAR.jar>