2012-08-16 63 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