0

Hello I have the following project structure.

enter image description here

I want to store my properties file outside my jar , so that if I change any property it will be reflected automatically. My property file contains ip of the other servers, which might change, so that's why I want to keep my properties file outside my jar.

My resources folder contains all my properties file including application.propeties.

I am using following snippet to read from my properties file.

server_1 = new Properties();

server_1.load(PropertyReader.class.getClassLoader().getResourceAsStream("resources/server_1.properties"));

server_2 = new Properties();

server_2.load(PropertyReader.class.getClassLoader().getResourceAsStream("resources/server_2.properties"));

I have also tried

server_2.load(PropertyReader.class.getClassLoader().getResourceAsStream("server_2.properties"));


server_2.load(PropertyReader.class.getClassLoader().getResourceAsStream("/server_2.properties"));

But it gives me following error :

Caused by: java.lang.NullPointerException: null

I have gone through the this link so that's why I have created resources folder at src level still I am not able to understand this behaviour.

Thank you

Salman Shaikh
  • 575
  • 1
  • 7
  • 24
  • You are confusing with the article which has absolute path. What your code does is checks for resources folder in the classpath. But your folder is outside your classpath. – Karthik R May 07 '18 at 05:45
  • Could you please elaborate more and if the issue is with the class path how it could be resolved. – Salman Shaikh May 07 '18 at 05:47
  • Added an answer as it contains bit of code. See if that helps. Note that the property path I have given is subject to your deployment structure and add the classpath entry according to your need. The classpath entry is relative to the jar's location. – Karthik R May 07 '18 at 05:56
  • Just noticed that it's a spring boot project. I have updated answer. mention the resources folder as a property and referencing it in application is the neat approach. – Karthik R May 07 '18 at 06:03

1 Answers1

0

To continue from my comment, If this a jar where you are separating resources outside the jar/bundle, you need the resources folder in the classpath of the jar.

For an executable jar in my project, I added a classpath entry(relative to the jar's location) in my jar's manifest entry via maven-jar plugin:

<plugin> 
   <artifactId>maven-jar-plugin</artifactId> 
     <configuration> 
       <archive> 
         <manifest> 
           <addClasspath>true</addClasspath> 
           <mainClass>....</mainClass> 
         </manifest> 
         <manifestEntries> 
           <Class-Path>../resources/</Class-Path> 
         </manifestEntries> 
       </archive> 
    </configuration> 
</plugin> 

Check the MANIFEST.MF file for the added entry. Then you can use in your code directly(without resources folder) :

PropertyReader.class.getClassLoader().getResourceAsStream("server_1.properties");

For your unit tests, you can directly provide in src/resources.

UPDATE:

Just noticed that you mentioned it's a spring boot application. Refer to this one. Use profiles to mention location of resources folder. This way it's neater.

Spring Boot Reading Properties Files Based on classpath arg

Karthik R
  • 5,523
  • 2
  • 18
  • 30