【问题标题】:Running jar file / .class file along with a properties file运行 jar 文件/.class 文件以及属性文件
【发布时间】:2014-02-05 11:17:18
【问题描述】:

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

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

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

但是当我运行 .class 文件时,我得到了这个错误:

无法读取属性文件...

我该怎么做?

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

【问题讨论】:

  • 将属性文件放在 .class 文件所在的位置
  • 试过没用

标签: java eclipse properties-file


【解决方案1】:

将属性文件放在您的类路径中,并将其作为资源读取。

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

【讨论】:

  • 谢谢你它工作但我不得不将属性文件移动到 src 文件夹中,在根目录中没有工作
  • @saeedhardan 因为如果您将其用作资源,它需要位于类路径中。
【解决方案2】:

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

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

而代码将类似于

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

【讨论】:

    猜你喜欢
    • 2017-10-14
    • 1970-01-01
    • 2015-05-04
    • 2013-11-14
    • 2014-06-07
    • 1970-01-01
    • 2012-02-05
    • 2014-10-02
    • 1970-01-01
    相关资源
    最近更新 更多