[英]Load resource file from outside of JAR file
使用maven
我正在构建一个必须动态加载驱动程序的应用程序。 用下面的代码就只有在工作driver.so
定位所产生的JAR文件中 。 我该怎么办,可以在JAR 之外的路径./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);
}
}
当驱动程序位于 JAR 内时,输出为:
jar:file:/home/ted/java/myproject/target/myproject-0.1-SNAPSHOT.jar!/libgdx64.so
当定位在 JAR 之外时 (在target
以及target/natives
目录中),输出为:
null
我通过以下方式启动应用程序:
cd /home/ted/java/myproject/target/
java -Djava.library.path=./natives -cp myproject-0.1-SNAPSHOT.jar com.myproject.Starter
我能做什么?
尝试这个:
package com.myproject;
public class Starter {
public static void main(String[] args) {
File file = new File("natives/driver.so");
System.out.println(file);
}
}
或这个:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.