Search code examples
javajava-native-interface

Can't find .dylib file after changing `java.library.path`


I created a .dylib file that I want to load using JNI and I can't get the Java program to find it.

I programmatically checked for the value of java.library.path and it was:

/Users/potato/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.

When I placed my .dylib file in one of these folders my Java program detected and loaded the .dylib file with no problems, but when I tried to change java.library.path to include the path to my desktop folder and placed the .dylib file on my desktop, the program couldn't find it.

System.setProperty("java.library.path", "/Users/potato/Desktop:" + System.getProperty("java.library.path"));
System.out.println(System.getProperty("java.library.path"));
System.loadLibrary("winfnc");

output:
/Users/potato/Desktop:/Users/potato/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no winfnc in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)

Any idea what am I doing wrong? Is there a way I can make the program find the .dylib file in any folder I want, or better yet in it's own .jar file and load it successfully?

EDIT: one answer offers a solution that uses the command line, but I'd like to be able to launch the application by double clicking the executable .jar file and not have to worry about any technicalities.

There is an option to add VM arguments in Eclipse, but they are only used when launching the program from Eclipse and not by directly opening the runnable .jar file.


Solution

  • java.library.path can't be changed from inside the program. You can set the property but it's value is only used on VM startup, and it will not be read again. You have to set it with -D

    See also my answer here: https://stackoverflow.com/a/51304696/2543253