Java classpath and config file

放肆的年华 提交于 2019-12-23 12:04:19

问题


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


回答1:


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.




回答2:


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");



回答3:


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

See:

  • How to add classpath entry for a jar file



回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/9412909/java-classpath-and-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!