简体   繁体   中英

Load resource file from outside of JAR file

With maven I'm building an application which has to load a driver dynamically. With the following code it only works if the driver.so is positioned inside the resulting JAR file. What can I do that the file can be found outside of the JAR within the path ./natives/driver.so .

package com.myproject;

public class Starter {
    public static void main(String[] args) {
        File classpathRoot = new File(Starter.class.getClassLoader().getResource("driver.so").getPath());
        System.out.println(classpathRoot);
    }
}

Output when driver is positioned inside JAR is:

jar:file:/home/ted/java/myproject/target/myproject-0.1-SNAPSHOT.jar!/libgdx64.so

Output when positioned outside JAR (in target as well as in target/natives directory) is:

null

I start the application via:

cd /home/ted/java/myproject/target/
java -Djava.library.path=./natives -cp myproject-0.1-SNAPSHOT.jar com.myproject.Starter

What can I do?

Try this:

package com.myproject;

public class Starter {
    public static void main(String[] args) {
        File file = new File("natives/driver.so");
        System.out.println(file);
    }
}

or this:

package com.myproject;

public class Starter {
    public static void main(String[] args) {
        File file = new File(System.getProperty("java.library.path"), "driver.so");
        System.out.println(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