如何处理打包成jar的Maven项目中资源的路径问题

4

我在将Maven项目打包成jar文件时遇到了资源管理问题(在Windows平台和Eclipse IDE上)。我不知道如何处理资源的路径。

我的资源在 /src/main/resources/ 文件夹中。

我的 pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>example.com</groupId>
  <artifactId>uploader</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>uploader</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
      <resources>
          <resource>
              <directory>src/main/resources</directory>
              <includes>
                  <include>clientconfig.xml</include>
                  <include>pause.png</include>
                  <include>resume.png</include>
                  <include>serverconfig.xml</include>
                  <include>stop.png</include>
              </includes>
              <!-- relative to target/classes
                   i.e. ${project.build.outputDirectory} -->
              <targetPath>..</targetPath>
          </resource>
      </resources>
  </build>

</project>

示例代码生成问题:

private static void readConfiguration() {
    try {
        String path = ClassLoader.getSystemResource("clientconfig.xml").toString();
        config = new UploaderConfig(path);
    } catch (ConfigException e) {
        e.printStackTrace();
    }
}

//...

public UploaderConfig(String path) throws ConfigException {
    File xmlFile = new File(path);
//...

我发现在导出文件后,文件被正确地存储到根jar文件归档中,但当我尝试从控制台调用程序时,我会得到以下错误信息:

java.io.FileNotFoundException: C:\Users\lenovo\Desktop\jar:file:\C:\Users\lenovo
\Desktop\uploader.jar!\clientconfig.xml (Nazwa pliku, nazwa katalogu lub sk│adni
a etykiety woluminu jest niepoprawna)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
        at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown So
urce)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrent
Entity(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineD
ocVersion(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(U
nknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(U
nknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown So
urce)
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown So
urce)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unk
nown Source)
        at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
        at example.com.uploader.config.UploaderConfig.<init>(UploaderConfig.j
ava:110)
        at example.com.uploader.client.Client.readConfiguration(Client.java:2
00)
        at example.com.uploader.client.Client.main(Client.java:222)
example.com.uploader.config.ConfigException
        at example.com.uploader.config.UploaderConfig.<init>(UploaderConfig.j
ava:119)
        at example.comt.uploader.client.Client.readConfiguration(Client.java:2
00)
        at example.com.uploader.client.Client.main(Client.java:222)

这不仅仅是关于这个特殊情况的问题,而是关于如何在存储和处理maven项目中打包到jar文件的资源文件时进行适当和正确的方法。


尝试使用"/clientconfig.xml"而不是"clientconfig.xml"。 - Eugene Ryzhikov
无法工作。getSystemSource返回null,这意味着Java找不到文件。请注意操作系统是Windows,因此路径分隔符为'\'。 - Piotr Zych
路径分隔符“/”是可移植的 - 适用于所有操作系统。尝试使用YourClass.class.getResource("/clientconfig.xml")。 - Eugene Ryzhikov
仍然是同样的错误。我检查了存档文件,确保clientconfig.xml文件在根目录中。java.io.FileNotFoundException: C:\Users\lenovo\Desktop\jar:file:\C:\Users\lenovo \Desktop\uploader.jar!\clientconfig.xml - Piotr Zych
1个回答

3

刚刚意识到...

当你的配置文件在jar包里时,你不能使用new File方法 - 因为这个文件并不存在。

可以使用YourClass.class.getResourceAsStream("/clientconfig.xml")方法,并从流中读取它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接