2013-02-20 46 views
0

我有一個獨立的應用程序,我將其作爲JAR文件導出,將其包含在運行於Tomcat的Web應用程序中。從Web應用程序中的jar文件讀取

的獨立應用程序從一個文件,該文件是包「配置」裏面讀取,通過這條線:

configFileName = Thread.currentThread().getContextClassLoader().getResource("config/indexer.cfg").getPath(); 

不幸的是這將導致在web應用程序的異常:

java.io.FileNotFoundException: file:\C:\apache-tomcat-7.0.23\IRSimWebApp\WEB-INF\lib\IR_Sim.jar!\config\indexer.cfg (The filename, directory name, or volume label syntax is incorrect) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at java.io.FileInputStream.<init>(Unknown Source) 

我注意到文件路徑中有一個感嘆號。這可能導致錯誤?它是如何到達那裏的?

+0

我想,是什麼你所做的是正確的。 exlclamation意味着它位於JAR內部。 – 2013-02-20 17:08:36

+0

該行不可能導致給定的異常。你是不是錯誤地在代碼中進一步下了新的FileInputStream(configFileName)?這將符合例外。 – BalusC 2013-02-20 18:40:44

+0

是的,它是由代碼後面的一個新的FileInputStream引起的 – PogoMips 2013-02-20 18:46:21

回答

1

好像你會通過獲取資源爲InputStream得到更好的服務:

InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config/indexer.cfg"); 
if (in != null) { 
    BufferedInputStream buff = new BufferedInputStream(in); 
    // process buff to get contents of file 
    buff.close(); 
} 

沒有異常處理上面,很明顯,但你應該明白我的意思...

相關問題