4

I have the problem running executable .jar file. I've created a project which contains a .properties file. It works just fine when I start it from eclipse, but when I export it to executable .jar file and try to run it with:

java -jar myfile.jar

I get the following exception:

(couldn't post image here) http://imageshack.us/photo/my-images/824/29583616.png/

I've checked my manifest file in the .jar and it contains the

Class-Path: .

And here's the properties file loading:

properties = new Properties();
    properties.load(new FileInputStream(
            "src/com/resources/treeView.properties"));

Any idea what causes this exception?

tenorsax
  • 21,123
  • 9
  • 60
  • 107
Ivo
  • 1,228
  • 1
  • 15
  • 33
  • 1
    try "properties = new Properties(); properties.load(new FileInputStream( "/com/resources/treeView.properties"));" without "src" in path. – happy Jun 23 '12 at 13:48

3 Answers3

5

If the properties file is inside the jar file, you cannot access it as a file.

You need to ask the classloader to get the resource as an inputstream. See Getting the inputstream from a classpath resource (XML file)

Community
  • 1
  • 1
Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Thanks! That worked! properties.load(ClassLoader.class.getResourceAsStream("/com/resources/treeView.properties")); – Ivo Jun 24 '12 at 08:40
1

In Eclipse (and in most IDEs) the current directory is the project's root directory. This means that Class-Path: . means something else in Eclipse than when you run it from the command line. This is why you wrote "src/com/...". Remove "src":

properties.load(new FileInputStream("com/resources/treeView.properties"));
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
0

Your properties file is within JAR file. So, use : ClassLoader.getResourceAsStream().

javaKid
  • 59
  • 2
  • 8