自执行jar和外部属性文件

时间:2012-08-16 09:21:46

标签: java jar

假设我有一个可执行jar文件的MANIFEST.MF的以下摘录:

Manifest-Version: 1.0
Main-Class: com.intersportleitner.skischule.gui.window.SkischulApplicationWindow
Class-Path: .
...

不应该有这样的目录结构:

Appdir
  |- bla.jar (self-executable)
  |- x.properties
  |- y.properties

因为如果我尝试使用以下代码片段加载属性,我会得到一个IOException:在properties.load(stream)中关闭了流:

Properties properties = new Properties();
InputStream istream=SkischulApplicationWindow.class.getClassLoader().getResourceAsStream("y.properties");
BufferedInputStream stream = new BufferedInputStream(istream);
properties.load(stream);
stream.close();

该异常有点误导,因为实际istream是null(注意,因为我试图调用istream的方法用于测试目的......),所以找不到属性文件,我不明白为什么它失败,因为根据Executable jar won't find the properties files,它应该以这种方式工作......

1 个答案:

答案 0 :(得分:2)

getResourcegetResourceAsStream依赖于类加载器实现&阶级路径。

当我在寻找嵌入式资源时,我倾向于只使用这种方法,但那只是我

你“可以”尝试

InputStream istream=SkischulApplicationWindow.class.getClassLoader().getResourceAsStream("/y.properties");

相反

相关问题