构建 jar 后的 Maven 资源文件
将 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 的根目录。
请在这里帮忙。我做错了什么?
After building maven project into jar, I get FileNotFoundException. I know that resources files are in the root directory after project being built thus I use this to get my file:
final File file = new File("maps/ParcelsCountyRDMFinal5.shp");
Here is the exception:
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)
The working directory here is obviously the directory from which I want to run jar. I guess that working directory should be root of the jar.
Please help here. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
然后您的文件位于您的类路径中。您可以尝试 Final File file = ClassLoader.getSystemResource("maps/ParcelsCountyRDMFinal5.shp").getFile();
Your file is then in your classpath. You can try final File file = ClassLoader.getSystemResource("maps/ParcelsCountyRDMFinal5.shp").getFile();
这就是你应该如何获取 jar 中捆绑的资源文件的绝对路径。
这对我有用..
THis is how u should get the absolute path for a resource file bundled inside the jar.
This works for me..