Java classpath and configuration file

I am having trouble finding a configuration file using classpath.

I use:

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

The properties file is located in the config directory.

When you run the program with eclipse, it works. I just added the configuration folder to the class path in the startup configuration.

But if I want to run the exported jar as follows:

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

I get a wonderful java.lang.NullPointerException because it cannot find the file.

It sounds classic and stupid, but I can't find any clues. What does an eclipse do, what am I not doing?

thanks

+4
source share
5 answers

You must use the absolute path for the class (if you do 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 make sure that the path to the directory containing this resource file is specified in the class path when jar starts so that the system loader can find this resource.

+2
source

To export Jar Files to access your resources ,

  • Right Click your project in Eclipse
  • Choose New -> Source Folder , now name it Source Folder as, say resources
  • Now manually add the config folder to this folder through the File System
  • Now go back to your Eclipse Right Click your project and select Refresh

Now you can see the added folder inside the Project Tree . Now, to access the contents of your configuration folder, write this:

 InputStream stream = myclass.class.getResourceAsStream("/resources/config/properties.file"); 
+1
source

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

See:

0
source

If you do not want to include the configuration file in your jar, you can pass the path to the configuration file as a command line argument for your application.

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

and then upload the file in the 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 this, since you will have a difficult deployment time. The file must exist for the target, and you must potentially run your program differently each time.

0
source

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 the jar file)

So, 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 class path before running your program:

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

Source: https://habr.com/ru/post/1398024/


All Articles