Reputation: 503
I have problem with resource managing in Maven project packed into jar file (on platform Windows + Eclipse IDE). I don't know how to handle path to resource.
My resources are in /src/main/resources/ folder.
My 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>
example code generating problem:
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);
//...
As i see after export files are correctly stored into root jar file archive but when i try to invoke program from console i get:
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)
(Sory about polish comments and line wrapping but i compile in polish-language system)
It is not only question about this special case, but abut how to propertly and correctly store and handle resources files in maven project packed to jar file.
Upvotes: 4
Views: 8085
Reputation: 17359
Just realized...
You cannot use new File
when your config is in your jar - the file does not exist.
Use YourClass.class.getResourceAsStream("/clientconfig.xml")
and read it from the stream.
Upvotes: 3