使用属性配置文件运行.jar文件

时间:2011-12-26 08:55:52

标签: java netbeans cmd configuration-files

我有jarproperties配置文件。

当我在Netbeans中运行它时,一切正常。

但是当我在cmd中运行时 - 找不到属性文件。

为什么?

2 个答案:

答案 0 :(得分:1)

这取决于您如何加载属性文件。 考虑在以下方面加载您的属性:

InputStream in = getClass().getResourceAsStream("/log4j.properties");

然后使用Properties.load(in)

这应该处理属性文件实际驻留在jar中的情况 祝你好运!

答案 1 :(得分:1)

当您在jar文件中打包属性时,您必须使用类加载器来找到该文件,因为它不再是可见作为文件。

如果属性文件位于jar文件的 root 的jar文件中,那么上面给出的答案就是你要使用的:

 Properties p = new Properties();
 InputStream is = MyClass.class.getResourceAsStream("/config.properities");
 if( is != null )
 {
    p.load(is);
 }

应该返回一个可以传递给Properities类加载的InputStream。如果该调用返回NULL,那么您需要查看属性文件相对于jar文件根目录的位置

相关问题