[英]How can I load an xml resource file from within an executible Jar file and save it to the folder the jar is located in?
[英]Is there a way to programmatically copy a resource file from within a JAR to an external folder?
我正在嘗試從當前正在運行的jar文件中訪問資源文件。 我現在需要的資源是一個jar文件,但是我需要一種適用於任何文件的方式。 我嘗試查看不同的解決方案,但是到目前為止,它們都沒有起作用。 我試圖使用URL指向jar中文件的絕對路徑,但這似乎不起作用。 我已經檢查了路徑,它應該正在獲取該文件。 無論我做什么,URL始終返回null。 我正在使用Apache Commons-IO。
到目前為止,這是我的代碼。
private static void copyFiles() {
URL source = ClassName.class.getClass().getResource("/resources/file.jar");
File target = new File(System.getProperty("user.home") +
System.getProperty("file.separator") + "Desktop" +
System.getProperty("file.separator") + "filename");
try {
FileUtils.copyURLToFile(source, target);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
這將引發NullPointerException。 任何幫助表示贊賞。 謝謝。
您可以使用Class.getResourceAsStream(... )來獲取該資源的InputStream(可以位於類路徑中的.jar內部或外部),然后將其寫入其他位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.