gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:07:55 24 4
gpt4 key购买 nike

我在打包到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com