[英]Adding a External Folder to the ClassPath in a runnable jar
我有一個 Maven 項目,我在其中使用 Maven 程序集插件創建了一個具有依賴項的 jar。 我還有一個外部配置文件 ( conf.properties ),jar 需要它才能正常工作。
我的項目結構是這樣的:
|- abc.jar
|- config
|-conf.properties
如何將此配置文件夾添加到 jar 文件的類路徑? 我嘗試使用-cp
命令並操作MANIFEST.MF
文件中的類路徑屬性來執行此操作,但到目前為止沒有運氣。
有誰知道這樣做的方法?
這就是我測試的方式(抱歉,沒有 maven)!
主要類:
package cfh.sf.Chamika;
import java.util.ResourceBundle;
public class ABC {
public static void main(String[] args) {
var bundle = ResourceBundle.getBundle("conf");
System.out.println(bundle.getString("test"));
}
}
清單文件,注意末尾的空行(條目必須以換行符結尾(CR、LF 或 CRLF)):
Manifest-Version: 1.0
Main-Class: cfh.sf.Chamika.ABC
Class-Path: config/
目錄結構
dist/
abc.jar
config/
conf.properties
conf.properties
內容:
test = OK, it is working!
執行
java -jar abc.jar
替代方案,不使用ResourceBundle
:
package cfh.sf.Chamika;
import java.io.IOException;
public class ABC {
public static void main(String[] args) {
try (var input = ClassLoader.getSystemResourceAsStream("conf.properties")) {
int ch;
while ((ch = input.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.