0

I have a jar file with properties configuration file.

when I run it in Netbeans everything works as it should.

but when I run it in a cmd - the properties file is not found.

why?

zeevblu
  • 1,059
  • 2
  • 11
  • 26

2 Answers2

1

It depends on how you're loading your properties file. Consider to load your properties with the help of:

InputStream in = getClass().getResourceAsStream("/log4j.properties");

and then use Properties.load(in)

This should handle the situation when the properties file physically resides in the jar Good Luck!

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • thanks, but i got an error about `getclass()` function: non-static method getClass() cannot be referenced from a static context – zeevblu Dec 26 '11 at 09:13
  • What is your error, the getClass() will work if you're not in the static method. – Mark Bramnik Dec 26 '11 at 09:14
  • If you're inside main method (which is static), and your class is named MyClass (for example), try this way: MyClass.class.getResourceAsStream(...) – Mark Bramnik Dec 26 '11 at 09:16
  • I think you better post the whole Exception stack trace here. This will help the community to figure out the issue. – Mark Bramnik Dec 26 '11 at 09:45
  • the `InputStream` in received a null. 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 myclass.myclass.main(myclass.java:31) – zeevblu Dec 26 '11 at 09:45
  • This means that you simply don't have such a properties file in your classpath. Do you run your binaries in jar or you're using *.class files directly? If you deal with jar, open it (a usual winzip/winrar will do the job) and ensure that you do have a properties file. If you don't use jar, figure out where does your property file reside and add this directory to the classpath (use java -cp option). – Mark Bramnik Dec 26 '11 at 13:01
1

When you package the properties inside the jar file, you have to use the class loader to locate the file since it is no longer visible as a file.

If the properties file is insider the jar file at the root of the jar file then the answer given above is what you would use:

 Properties p = new Properties();
 InputStream is = MyClass.class.getResourceAsStream("/config.properities");
 if( is != null )
 {
    p.load(is);
 }

that should return an InputStream that you can pass to the Properities class to load. If that call returns a NULL then you need to see where the property file is relative to the root of the jar file.

Jere
  • 581
  • 2
  • 3