我在将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文件的资源文件时进行适当和正确的方法。