【问题标题】:Self-executable jar and external property-files自执行 jar 和外部属性文件
【发布时间】:2012-08-16 09:21:46
【问题描述】:

假设我有一个可执行 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: Stream closed in 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 为空(指出当我试图调用 istream 的方法进行测试时...),所以找不到属性文件,我不知道为什么失败是因为根据Executable jar won't find the properties files 它应该以这种方式工作......

【问题讨论】:

  • 你是用AppDir作为执行目录吗?我的意思是,你在执行:java -jar bla.jar standing 在那个目录上吗?

标签: java jar


【解决方案1】:

getResourcegetResourceAsStream 依赖于类加载器实现和类路径。

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

你“可以”尝试

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

相反

【讨论】:

  • 嗨,感谢您的回答,如果我在实际文件名之前添加“/”有帮助,我也尝试过,但它仍然失败。 @helios:是的,示例中的 Appdir 是当前工作目录,我像这样执行它 java -jar bla.jar
猜你喜欢
  • 2012-04-25
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 2014-12-30
  • 2016-04-03
  • 1970-01-01
相关资源
最近更新 更多