Why my Java application cannot find the properties file

I have a simple application that reads and writes to a properties file. It was developed at NetBeans, and when run from Netbeans it works fine. However, now I created and deployed it, and the properties file was not found.

Project structure

com/company/projectname/controller/controllerclass.java <- this is where the code using the properties file exists conf/runController.properties <- the properties file, this exists here 

In the code, I have the following for accessing the properties file:

 private final String PROPERTIESFILELOCATION = "conf/runController.properties"; public void runController(){ this.runController = new Properties(); this.propertiesLocation = this.getClass().getClassLoader().getResource(this.PROPERTIESFILELOCATION); FileInputStream propsInput = new FileInputStream(this.propertiesLocation.getFile()); this.runController.load(propsInput); } 

(summary for brevity)

When I call the jar file from the command line, I output:

 java -classpath /usr/java/projectdirectory/project.jar com.company.projectname.controller.controllerclass arg1 

So, I managed to achieve this earlier in other projects this way, but for some reason this does not work.

I checked the structure inside the jar file and everything is there as expected.

Can someone point out my mistake and help me deal with this?

EDIT - changed the names to match each other. They were always in my code.

+7
source share
4 answers

Thanks to everyone for the help, especially dashrb, plus 1 for your code.

I managed to get it to work with your help. The final solution to this problem was a slight change in course.

As I needed to read and write to a file (perhaps not clear in my OP), I switched to using Apache.commons.configuration.

However, the pointers in this thread guaranteed that my other property files would work without crashing.

Thanks again

0
source

Your question says about

Conf / controller.properties

and your code says conf/runController.properties

EDIT: I assume the conf / * properties are inside your jar file. If so, then your code should work if the file names are specified correctly.

+3
source

Perhaps FileInputStream is not able to evaluate the properties of the β€œfile” that is inside your jar file. Change:

 this.runController = new Properties(); this.propertiesLocation = this.getClass().getClassLoader().getResource(this.PROPERTIESFILELOCATION); FileInputStream propsInput = new FileInputStream(this.propertiesLocation.getFile()); this.runController.load(propsInput); 

in

 this.runController = new Properties(); this.runController.load(this.getClass().getClassLoader().getResourceAsStream(this.PROPERTIESFILELOCATION)); 

EDIT: I created a test class, and it shows that when working from the file system or from the JAR file, this "props / main.properties" works, but "/props/main.properties" does not work:

 [ rtb@rtblinux props]$ cat org/dashrb/test/main.java package org.dashrb.test; import java.util.Properties; import java.io.IOException; public class main { public static void main(String args[]) { main myMain = new main(); myMain.testProps("props/main.properties"); myMain.testProps("/props/main.properties"); } public main() { } public void testProps(String p) { try { System.out.println("==============================="); Properties props = new Properties(); System.out.println("Trying to load properties as " + p); props.load(getClass().getClassLoader().getResourceAsStream(p)); System.out.println("Loaded properties as " + p); System.out.println("Property x is: " + props.getProperty("x")); } catch (IOException ioe) { ioe.printStackTrace(); } System.out.println("==============================="); } } [ rtb@rtblinux props]$ cat props/main.properties x = This is the property value of x [ rtb@rtblinux props]$ java -cp . org.dashrb.test.main =============================== Trying to load properties as props/main.properties Loaded properties as props/main.properties Property x is: This is the property value of x =============================== =============================== Trying to load properties as /props/main.properties 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 org.dashrb.test.main.testProps(main.java:25) at org.dashrb.test.main.main(main.java:11) [ rtb@rtblinux props]$ jar cvf main.jar org props added manifest adding: org/(in = 0) (out= 0)(stored 0%) adding: org/dashrb/(in = 0) (out= 0)(stored 0%) adding: org/dashrb/test/(in = 0) (out= 0)(stored 0%) adding: org/dashrb/test/main.class(in = 1218) (out= 679)(deflated 44%) adding: org/dashrb/test/main.java(in = 594) (out= 287)(deflated 51%) adding: props/(in = 0) (out= 0)(stored 0%) adding: props/main.properties(in = 36) (out= 36)(deflated 0%) [ rtb@rtblinux props]$ jar tvf main.jar 0 Fri Jan 11 17:29:40 EST 2013 META-INF/ 68 Fri Jan 11 17:29:40 EST 2013 META-INF/MANIFEST.MF 0 Fri Jan 11 17:26:00 EST 2013 org/ 0 Fri Jan 11 17:26:00 EST 2013 org/dashrb/ 0 Fri Jan 11 17:29:24 EST 2013 org/dashrb/test/ 1218 Fri Jan 11 17:28:52 EST 2013 org/dashrb/test/main.class 594 Fri Jan 11 17:29:24 EST 2013 org/dashrb/test/main.java 0 Fri Jan 11 17:26:40 EST 2013 props/ 36 Fri Jan 11 17:26:40 EST 2013 props/main.properties [ rtb@rtblinux props]$ cd / [ rtb@rtblinux /]$ java -cp ~/misc/src/java/props/main.jar org.dashrb.test.main =============================== Trying to load properties as props/main.properties Loaded properties as props/main.properties Property x is: This is the property value of x =============================== =============================== Trying to load properties as /props/main.properties 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 org.dashrb.test.main.testProps(main.java:25) at org.dashrb.test.main.main(main.java:11) 

There must be something else in your situation that interferes with your success.

+3
source

If you use getResource to open a properties file, the file is assumed to be in the class path, so you need to put the properties file in the class path. Alternatives are to add the conf directory to the class path or move the properties file so that it is in the existing class path.

One thing that can help is to refer to the location of the file with the start slash, so there is no doubt that you want to start searching for the file from the root of the class path. Otherwise, the search path refers to the class from which your code makes the call (I don’t know if this is correct, since it works with Class.getResource, Classloader.getResource may be different).

+2
source

All Articles