java - 如何处理打包到jar的maven项目中的资源路径

标签 java eclipse maven jar resources

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

我的资源在/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)

(对波兰语注释和换行感到抱歉,但我在波兰语系统中编译)

不仅是这种特殊情况的问题,还涉及到如何在打包成jar文件的maven项目中正确存储和处理资源文件。

最佳答案

刚刚意识到...

当您的配置在您的 jar 中时,您不能使用 new File - 该文件不存在。

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

关于java - 如何处理打包到jar的maven项目中的资源路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16251645/

相关文章:

java - 使用消息捕获并处理 Java 中的异常抛出(打开文件过多)

java - 在我的 java 项目中使用其他 java 文件

java - 如何从 url 视频链接连续重复同一视频?

java.lang.IllegalStateException : The specified child already has a parent. 您必须先在 child 的 parent 上调用 removeView()

c++ - Eclipse Indigo CDT 代码完成

android - 从Android Studio读取txt、mp3等文件

maven - Maven 上的依赖冲突

java - Spring 4中如何使用fasterxml Jackson JSON序列化

java - Maven 取消禁止依赖项(为了使用库)?

java - OGNL exp 未评估其日期值