Download the .properties file from the IDE as well as from outside the JAR

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:)

+5
source share
2 answers

How do you work at the moment when you depend on the working directory of your java process. Typically, your command line (aka shell) indicates when the application starts.

In most IDEs, you can configure this directory in the startup settings. (For an eclipse, this is on the second tab "Arguments") Thus, you need the target directory (for eclipse there is a button "Workspace ...")

In particular, in intelliJ you can find this parameter in the Run / Edit Configurations section ... This will open a window as follows: enter image description here There you can edit the working directory. You just add the target to the end.

Edit: In fact, you add src / main / ressources at the end.

0
source

In your Java code, read your app.properties file as follows:

 final String ROOT_PATH = "custom.path"; // Or whatever you most like final String PROPERTIES_FILE = "app.properties"; // start :: try-catch here to manage I/O resources File directory = new File(System.getProperty(ROOT_PATH), "conf"); File file = new File(directory, PROPERTIES_FILE); BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); // ... // end :: try-catch here to manage I/O resources 

Then you need to set / declare custom.path :

  • Declaring it as an environment variable
  • Setting it at runtime (environment properties) using -Dcustom.path=/somewhere/in/your/filesystem (I prefer this option unless the path is fixed and / or used for different applications)

Then, in the end, you need to copy / place your app.properties file inside /somewhere/in/your/filesystem/conf . Why inside /conf ? Because you use it when declaring a directory field. If you do not want this, just do not install it and do not remove the , "conf" .

In addition, to run “locally” (in your IDE), use the VM Settings (IntelliJ IDEA) parameter:

enter image description here

0
source

All Articles