简体   繁体   中英

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

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 eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM