构建jar后的Maven资源文件

时间:2012-02-15 19:19:19

标签: maven resources build jar

将maven项目构建到jar后,我得到了FileNotFoundException。我知道在构建项目之后资源文件位于根目录中,因此我使用它来获取我的文件:

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的根目录。

请在这里帮忙。我究竟做错了什么?

2 个答案:

答案 0 :(得分:0)

然后您的文件位于类路径中。您可以尝试最终的File file = ClassLoader.getSystemResource(“maps / ParcelsCountyRDMFinal5.shp”)。getFile();

答案 1 :(得分:0)

这就是你应该如何获得jar中捆绑的资源文件的绝对路径。

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

这对我有用..