[英]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.