2012-08-16 48 views
0

假设我有一个可执行的JAR文件的MANIFEST.MF以下摘录:自我可执行的JAR和外部的财产文件

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关闭(流):

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应该以这种方式工作...

+0

是否使用APPDIR作为执行目录?我的意思是,你正在执行:'java -jar bla.jar' *站在*那个目录下? – helios 2012-08-16 09:24:21

回答

2

getResourcegetResourceAsStream是类加载器实现&类路径依赖上。

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

你“可能”尝试

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

相反

+0

嗨,谢谢你的回答,我也试过,如果它有帮助,如果我添加一个'/'之前的实际文件名,它仍然失败。 @helios:是像这个例子中的Appdir是当前工作目录,我执行它像这样java -jar bla.jar – spielc 2012-08-17 06:24:29