2012-02-15 31 views
0

將maven項目構建到jar之後,我得到FileNotFoundException。我知道,資源文件都在根目錄下的項目被從而建立後,我用它來得到我的文件:構建jar之後的Maven資源文件

final File file = new File("maps/ParcelsCountyRDMFinal5.shp"); 

這裏是個例外:

C:\Users\ilija\Desktop\MavenizedCP\cp-map\target>java -jar cp-map-0.0.1-SNAPSHOT 
-shaded.jar 

Exception in thread "main" java.io.FileNotFoundException: C:\Users\ilija\Desktop 
\MavenizedCP\cp-map\target\maps\ParcelsCountyRDMFinal5.shp (The system cannot fi 
nd the path specified) 

這裏的工作目錄,顯然是目錄從中我想運行jar。我想工作目錄應該是jar的根目錄。

請幫助這裏。我究竟做錯了什麼?

+0

究竟哪裏是你的ParcelsCountyRDMFinal5.shp文件?它是在C:\ maps \ ParcelsCountyRDMFinal5.shp還是它在資源目錄中的maven項目的一部分? – eigil 2012-02-15 19:44:06

+0

它是我的maven項目資源目錄的一部分! – ilija 2012-02-15 19:49:40

回答

0

然後你的文件就在你的類路徑中。你可以嘗試最終的File file = ClassLoader.getSystemResource(「maps/ParcelsCountyRDMFinal5.shp」)。getFile();

+1

線程「main」中的false異常java.io.FileNotFoundException:C:\ Users \ ilija \ Desktop \ MavenizedCP \ cp-map \ target \ file:\ C:\ Users \ ilija \ Desktop \ MavenizedCP \ cp-map \ targe t \ cp-map-0.0.1-SNAPSHOT.jar!\ maps \ ParcelsCountyRDMFinal5.shp(文件名,目錄名或卷標語法不正確)這是我在嘗試提議後得到的結果。第一個錯誤是由於我檢查過的函數file.canRead()。 – ilija 2012-02-15 20:21:23

0

這是你應該如何獲得jar中捆綁的資源文件的絕對路徑。

URI fileURI = Thread.currentThread().getContextClassLoader() 
       .getResource("ParcelsCountyRDMFinal5.shp").toURI(); 
filepath = new File(fileURI).getAbsolutePath(); 

這對我的作品..

相關問題