How to handle the resource path in a maven project packaged in a jar

I have a problem with resource management in a Maven project packaged in a jar file (on the Windows + Eclipse IDE platform). I do not know how to handle the path to the resource.

My resources are located in the / 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 ie ${project.build.outputDirectory} --> <targetPath>..</targetPath> </resource> </resources> </build> </project> 

Code Generation Example:

 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 can see, the export files are stored correctly in the root jar file archive, but when I try to call the program from the 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 skadni 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 string wrappers, but I compile on the system in Polish)

This is not only a question about this special case, but also how to properly and correctly store and process resource files in a maven project packaged in a jar file.

+4
source share
1 answer

Just got it ...

You cannot use new File when your configuration is in your bank - the file does not exist.

Use YourClass.class.getResourceAsStream("/clientconfig.xml") and read it from the stream.

+3
source

All Articles