0

I am dependent on a library src/main/resources/libs64/thelib.dll which I need to load at runtime

System.setProperty("java.library.path", 
                    Paths.get(System.getProperty("user.dir"),
                      "src", "main", "resources", "libs64").toAbsolutePath().toString())

When I run the spring boot app

mvn package
java -jar springapp.jar

I tells me

java.lang.UnsatisfiedLinkError: no src/main/resources/libs64/thelib in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) ~[na:1.8.0_92]
    at java.lang.Runtime.loadLibrary0(Runtime.java:870) ~[na:1.8.0_92]
    at java.lang.System.loadLibrary(System.java:1122) ~[na:1.8.0_92]

When I set the library path src/main/resources/libs64 in Eclipse it works and the java.library.path prints out to the same as

Paths.get(System.getProperty("user.dir"),
                          "src", "main", "resources", "libs64").toAbsolutePath().toString())

Thanks for the help

1 Answer 1

1

src/main/resources/libs64 is the path to the library in your Eclipse project. On a system where your Eclipse workspace is not present this will never work.

When you package your app into a Jar the library (as a resource) will be copied into that jar, somewhere below the path libs64.

AFAIK Java can't load libraries which reside inside of a zip/jar file. Therefore you have to extract to e.g. to the temp directory and then load it from there. Or alternatively you don't pack the library into the Jar file and just deploy it next to the JAR file. Then you can load it directly from the install location of your JAR file.

5
  • I tried again with loading it from a location outside the jar via System.setProperty("java.library.path", "C:\\Username\\libs64"); and triple checked that the folder location is correct but it still gives the same error as above
    – tenticon
    Mar 27, 2018 at 17:44
  • BTW. Why do you set the Library path. Just load the DLL directly you already know the exact path: System.load("C:\\Username\\libs64\\thelib.dll");. This way it makes less problems.
    – Robert
    Mar 27, 2018 at 18:00
  • Because a java library on which I am depending is loading the .dll libraries. I just need to provide the correct java.library.path
    – tenticon
    Mar 27, 2018 at 18:09
  • That does not matter. You can load the dll in your code before the java-library code tries to load it. Java loads libraries only once (identified by the library name). If it has been loaded successfully further loading attempts will be ignored (no exception).
    – Robert
    Mar 27, 2018 at 18:15
  • 1
    In the end only -Djava.library.path worked. I debugged the System.loadLibrary() function and I see that it doesnt re-initialize the user path (from which it reads the libraries) once the system path (path of the jdk) is already set. So I once the jvm is running it seems there is no way to alter the java.library.path!
    – tenticon
    Mar 28, 2018 at 9:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.