Skip to content
Advertisement

Read properties file from classpath (non-maven)

When I try to run my jar from another directory it cannot see the “config” folder with the “url.properties” file in it.

Inside MyProperties class I have the following code, which runs perfectly when run from the same dir:

Properties properties = new Properties();
FileInputStream in = new FileInputStream("./config/url.properties");
properties.load(new InputStreamReader(in, Charset.forName("UTF-8")));
in.close();

The class that contains this code is in the following tree:

myprojectMyAppClass.class
myprojectdataMyProperties.class

It runs fine if I run this jar which contains the above piece of code by doing:

C:myjarfolder>java -jar myApp.jar

But it does not find “url.properties” inside “config” and returns a error if I do:

C:>java -jar c:myjarfoldermyApp.jar

Obviously it seems a classpath problem, so I try the following without success and return the same “file not found” error:

C:> java -cp "c:myjarfolder*;c:myjarfolderconfig" myproject.MyAppClass
C:> java -cp "c:myjarfolder*;c:myjarfolderconfig" myproject.MyAppClass
C:> java -cp "c:myjarfolder*;config" myproject.MyAppClass
C:> java -cp "c:myjarfolder*;config" myproject.MyAppClass
C:> java -cp "c:myjarfolder*;config" myproject.MyAppClass
C:> java -cp "c:myjarfolder*;c:myjarfolderconfigurl.properties" myproject.MyAppClass 

And many other variants with and without quotes, all without success.


Then I thought it could be a loaded resources problem and tryed to change the InputStream(its in a static method):

Properties properties = new Properties();
properties.load(MyProperties.class.getClassLoader().getResourceAsStream("./config/url.properties"));

and then

properties.load(MyProperties.class.getClassLoader().getResourceAsStream("config/url.properties"));

This way it does not work even when running in the same folder.


I also tried to put the config folder in the class-path: inside manifest. Dind’t worked.


I tried many things I found here on stack and none seems to work.

Is there a way to make the first option (without getresources) to run from another dir?

If I somehow make it load from getResource, will it run from another dir if I refer the class path?

Advertisement

Answer

When using absolute locations like FileInputStream(“./config/url.properties”):

Add the config folder inside the jar. From the IDE you can add it as a source and it will compile the folder inside the jar automatically. Then it can be run with “java -jar c:myjarfoldermyJar.jar” from another folder no problem.


When using getResources(“url.properties”):

Can’t use the absolute location here or it will be the same problem as before, so to let the folder outside the jar and add it to the classpath (you may not run it from the IDE if not added to the run parameters classpath or the manifest). The folder can be added by running it like:
‘java -cp “c:myjarfolder*;c:myjarfolderconfig” myproject.MyAppClass’

Also, it can be added to the MANIFEST.MF class-path, as example “class-path: config/”. (it only worked here with “/”) and can be run with “java -jar c:myjarfoldermyJar.jar”.


I don’t think this is a normal issue people will encounter because normaly you run the jar from the same location it is. But we were creating a RDP to run from server where it can’t run the jar it self, but using the java.exe.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement