【发布时间】:2012-04-25 01:34:06
【问题描述】:
我有一个带有 main-class 的 jar,可以像这样执行:java -jar test.jar
在罐子里我有类似的东西
public static void main(String[] args) throws IOException {
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("config.properties");
Properties prop = new Properties();
prop.load(is);
//then I wanna fetch all the properties in the config.properties file
}
我两个都跑:
java -jar test.jar
java -jar test.jar -cp /tmp(config.properties 所在的位置)
java -jar test.jar -cp /tmp/config.properties(显然它不起作用,但让你知道我在这里想要实现的目标)
代码不起作用,尽管我将 config.properties 文件的路径放在我的 $PATH 和 $CLASSPATH 下,但所有三个都抛出 NPE。
关键是,从长远来看,我会将配置文件放在 ${my-config-path} 中,并正确读取/处理它。但暂时我只想要一些快速而肮脏的东西。
- 我不想在我的 jar 中包含属性文件。
- 我想将它保存在外部的类路径或路径中,当我执行 jar 时,它会毫无问题地找到它。
【问题讨论】:
-
new FileInputStream(new File("config.properties"))? -
你没有文件的路径,你会得到 java.io.FileNotFoundException。
-
我想你可以在 SO 中找到如何使用
$PATH和我的评论来实现这一点。 -
它已移交给 QA 团队,因此他们可以选择自己想要的路径。所以暂时让他们使用系统属性-Dconfig.path ^^
标签: java properties jar