tenticon
tenticon

Reputation: 2913

java - loading library from java library path

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

Upvotes: 0

Views: 2364

Answers (1)

Robert
Robert

Reputation: 42575

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.

Upvotes: 1

Related Questions