Search code examples
javajarjava-native-interfaceexecutable-jar

"Native code library failed to load" outside of IDE when running exported .jar


as stated in the title, my programm won't start outside of Eclipse. I've imported a library consisting of a xy.jar and xy_jni.dll

CMD within the folder of the jar-file:

java -jar myprogramm.jar

is followed by the statement:

"Native code library failed to load. Please add the xy-folder as location for native libraries. java.lang.UnsatisfiedLinkError: no xy_jni in java.library.path

It doesn't matter if I click extract or package ressources when exporting the jar-file.

Also under Project-Settings, Java Build Path, Libraries the .jar & xy_jni(.dll) is added and within all the expandables in libraries the "xy-folder" is set as "Native library location". Also under Libraries I added a "Add Library" named "xy-folder" inhibiting the .jar & xy_jni.dll

When opening the final exported jar-file with a file viewer the xy-folder is included with the xy_jni.dll only, the xy.jar is in the root folder.

What am I doing wrong? Thank you very much in advance!


Solution

  • It's not easy to distribute dll/so/dylib within jar file. The reason here is that you depend on system when it comes to loading libraries.

    Typically, libraries have to lay around, somewhere on the file system. This way, they are loaded by System.load and System.loadLibrary.

    You can use simple solution to solve this issue. All you have to do, is to extract your native code to some location and load it from there.

    Take a look here for a full sample related to loading libraries from JAR:

    https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo035