运行jar文件/ .class文件以及属性文件

时间:2014-01-16 13:51:39

标签: java eclipse properties-file

我有一个使用Properties文件的eclipse项目。该文件位于项目的根目录中。我希望能够使用cmd(在Windows中)运行eclipse构建的.class文件。

我使用此代码从文件中获取属性:

Properties prop = new Properties();
FileInputStream fileStream = new FileInputStream("DBConfig.properties");
prop.load(fileStream);

但是当我运行.class文件时,我收到此错误:

  

无法读取属性文件...

我该怎么做?

我也确实看到了类似的帖子,但他需要覆盖系统属性,我不会:Execute a jar with an external properties file

2 个答案:

答案 0 :(得分:2)

将属性文件放在类路径上并将其作为资源读取。

如果您需要提供外部配置(例如,可以通过编辑文本文件进行更改),请使用命令行参数或系统参数(例如-Dconfig=/a/full/path)来查找文件。

答案 1 :(得分:1)

尝试将属性文件作为参数传递,如下面的

java -jar <jar name> -DDBConfig.properties=<Path>

代码就像

FileInputStream fileStream = new FileInputStream(System.getProperty("DBConfig.properties"));