簡體   English   中英

從JAR文件外部加載資源文件

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM