4
votes

I'm having some trouble finding a config file with classpath.

I use :

InputStream stream = myclass.class.getResourceAsStream("properties.file");

The properties.file is located under config directory.

When running the program with eclipse, it works. I just added config folder in the classpath in the launch configuration.

But If I want to run the exported jar like this :

java -jar -cp C:\project\lib;C:\project\config myclass.jar

I get the oh wonderful java.lang.NullPointerException because it can't find the file.

This sounds classic and stupid but I can't find a clue. What does eclipse do that I don't ?

Thanks

5

5 Answers

2
votes

You should use absolute path for class (If you don not want to put the properties file in your jar relative to the class)

InputStream stream = myclass.class.getResourceAsStream("/properties.file");

or simply

InputStream stream = myclass.class.getClassLoader().getResourceAsStream("properties.file");

And then ensure that the path to the dir containing this resource file is specified in the classpath when you run the jar so that the systemclassloader will be able to find this resource.

1
votes

when you launch jar file using java -jar it ignores the classpath specified by -cp

See:

1
votes

In order for your Exported Jar Files to access your resources,

  1. Right Click your Project in Eclipse
  2. Select New -> Source Folder, now name this Source Folder as, say resources
  3. Now manually add your config folder to this folder through File System
  4. Now go back to your Eclipse Right Click your Project and Select Refresh

Now one can see the added folder inside the Project Tree. Now in order to access the contents of your config Folder write this :

InputStream stream = myclass.class.getResourceAsStream("/resources/config/properties.file");
0
votes

If you don't want to include the config file in your jar, you could pass the path to the config-file as a command line argument to your application.

java -jar myclass.jar C:\project\config 

and then load the file by absolute path

public static void main(String[] args)
{
   String pathToConfig = args[1]; //or 0/2, check to see
   String configFilePath = pathToConfig  + "/properties.file";
   String[] lines = FileUtil.ReadLines(configFilePath);

}

But I recommend against it, since you are going to have a tough time deploying. The file must exist at the target and you have to potentially start your program differently each time.

0
votes

If you type java -help you will see the following USAGE:

Usage: java [-options] class [args...] (to execute a class)
or java [-options] -jar jarfile [args...] (to execute a jar file)

So maybe you will need to fix the command as follows:

java -cp C:\project\lib;C:\project\config -jar myclass.jar

You can also try the following:

java -classpath C:\project\lib;C:\project\config -jar myclass.jar

Or even setting the classpath before running your program:

set CLASSPATH=C:\project\lib;C:\project\config
java -jar myclass.jar