I have an app.properties file in my maven project in the resources folder, as shown here (simplified):
myapp
| ---- src
| |
| | --main
| | --java
| | | --ApplicationInitializer.java
| |
| | --resources
| | --app.properties
|
| --- target
| --myApp.jar
| --app.properties
In the ApplicationInitializer class, I want to load properties from the app.properties file using the following code snippet:
Properties props = new Properties(); String path = "/app.properties"; try { props.load(ApplicationInitializer.class.getResourceAsStream(path)); } catch (IOException e) { e.printStackTrace(); } System.out.println(props.getProperty("property"));
This piece of code loads properties correctly when I run it from within my IDE, but it is thrown with an error
Exception in thread "main" java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properties.java:353) at java.util.Properties.load(Properties.java:341) at cz.muni.fi.fits.ApplicationInitializer.main(ApplicationInitializer.java:18)
when trying to run as a jar file.
To create a jar file, I use a combination of maven-shade-plugin , maven-jar-plugin (to exclude the properties file outside the JAR) and maven-resources-plugin (to copy the properties file to a specific folder) in the pom.xml file, as shown below:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>cz.muni.fi.fits.ApplicationInitializer</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.5</version> <configuration> <excludes> <exclude>**/*.properties</exclude> </excludes> </configuration> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <executions> <execution> <id>copy-resource</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target</outputDirectory> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> </configuration> </execution> </executions> </plugin>
Then I switched the code in the main method to this:
Properties props = new Properties(); String path = "./app.properties"; try (FileInputStream file = new FileInputStream(path)) { props.load(file); } catch (IOException e) { e.printStackTrace(); } System.out.println(props.getProperty("property"));
and it was possible to load the properties from the file when the JAR started, but this time I could not load them when working in the IDE, it ended with the same exception as above.
So my question is: How to set the file path (or pom.xml file?) So that I can load the properties launched from the IDE and JAR file?
Thanks in advance:)