2011-11-21 101 views
0

我導出了一個讀取2個配置文件的項目。然而,當我執行jar時,它會得到這個錯誤:Jar文件:在jar中加載配置文件

Error: java.io.FileNotFoundException: config/dbConnection.config (No such file or directory) 
    Error: java.io.FileNotFoundException: config/test.config (No such file or directory) 
    com.smart.ets.services.ServiceException: com.smart.ets.services.ConfigErrorException 
     at com.smart.ets.services.TransactionServiceImpl.getConfigData(TransactionServiceImpl.java:30) 
     at com.smart.ets.controllers.Simulator.getTransactionSimulationResults(Simulator.java:41) 
     at com.smart.ets.controllers.Simulator.main(Simulator.java:26) 
    Caused by: com.smart.ets.services.ConfigErrorException 
     ... 3 more 
    [email protected]:~/Documents/Dropbox$ java -jar ETS.jar 
    Error: java.io.FileNotFoundException: config/dbConnection.config (No such file or directory) 
    Error: java.io.FileNotFoundException: config/test.config (No such file or directory) 
    com.smart.ets.services.ServiceException: com.smart.ets.services.ConfigErrorException 
     at com.smart.ets.services.TransactionServiceImpl.getConfigData(TransactionServiceImpl.java:30) 
     at com.smart.ets.controllers.Simulator.getTransactionSimulationResults(Simulator.java:41) 
     at com.smart.ets.controllers.Simulator.main(Simulator.java:26) 
    Caused by: com.sm 

art.ets.services.ConfigErrorException 
    ... 3 more 

有什麼辦法解決這個問題?它運作,如果我運行項目(不是項目的jar文件)

回答

4

你忽略提及你如何訪問文件,但從錯誤它看起來像你試圖訪問它們作爲文件 - 但他們不是,他們是類路徑上的資源。因此,您應該使用getResourceAsStream。資源的名稱將是包相對路徑。