2012-02-15 32 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(); 

这对我的作品..

相关问题