【问题标题】:Maven resources files after building jar构建jar后的Maven资源文件
【发布时间】:2012-03-07 04:09:48
【问题描述】:

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

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

【问题讨论】:

  • 您的 ParcelsCountyRDMFinal5.shp 文件到底在哪里?它在 C:\maps\ParcelsCountyRDMFinal5.shp 中还是在资源目录中的 maven 项目中?
  • 它是我的 maven 项目资源目录的一部分!

标签: maven resources build jar


【解决方案1】:

你的文件然后在你的类路径中。你可以试试 final File file = ClassLoader.getSystemResource("maps/ParcelsCountyRDMFinal5.shp").getFile();

【讨论】:

  • 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() 造成的。
【解决方案2】:

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

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

这对我有用..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2023-04-03
    • 2015-09-27
    相关资源
    最近更新 更多