Take the 2-minute tour ×

First of all, sorry if the title is not as descriptive as it should be, but it was kind of difficult to explain it in such a short way.

I have a Java application (non-web) which must read from a properties file. This Java application must as well be exported as a JAR file and then is used in a web application, but the properties file is not into the JAR file. It would look like this:


So, when coding the Java application that will be exported as myJavaApp.jar I am unable to do it so it would read the config.properties file from a Relative path. If I put an Absolute path it works correctly:

properties.load(new FileInputStream("C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/MyWebApp/WEB-INF/config.properties"));

But, if I try to use a relative path it doesn't work. I've tried different solutions from here or other webpages but none of them are working.

Almost forgot, the class where I am reading the properties file is static!


Thanks in advanced.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

If you make sure the file is on the classpath, then you can simply load it with YourClassName.class.getResourceAsStream("/config.properties"). That would be the easiest way of implementing it. For your webapplication, you put the file in WEB-INF/classes/, and for your standalone application you simply set the classpath when starting the application.

share|improve this answer
It is working! I added ../../../config/config.properties in order to put the properties file at the top of the web application directory. The problem now is that any modification on the config file needs Tomcat to be restarted in order to apply :(. Is there any way to avoid this? Because when I used the absolute path I was able to modify the config file 'on the fly'. Thanks –  Jinka Feb 10 '12 at 12:13
You don't want to use the relative path ../../../config/config.properties. The classpath (or maybe a subdir) is the perfect place for this resource to live. –  Matthijs Bierman Feb 10 '12 at 13:58
As for Tomcat having to be restarted, maybe it is using antiResourceLocking, causing everything to be copied somewhere else? –  Matthijs Bierman Feb 10 '12 at 14:00
Thanks for your help. I am putting the config file in such a folder in order to be easier for maintenance people to find it, although if there is any implication about this change I will swap it again to the classpath. Regarding antiResourceLocking, Tomcat configuration is default, so I do not know what can be happening. I've been reading some doc about antiResourceLocking and I've been trying to set it to false or true but it is not 'reloading' properties neither way :/ –  Jinka Feb 13 '12 at 8:47
add comment

In your Java Application, you can get the absolute path of current directory by:


Assuming you have config.properties residing in the same directory, you should be able to figure out the absolute path of this file in your java code and you can deploy it anywhere, without hard-coding the absolute path in your code.


If your Java App is an executable JAR, above command will return correct directory only if it is directly invoked. If you invoke this application indirectly from another process, on a windows platform, the starting directory will be windows/system32, So you have to analyze how your jar is being invoked. If latter is the case, it is better to add functionality in your web app to provide jar with the path of your properties file.

share|improve this answer
It worked when testing the Java application but once it is exported as a JAR and executed into the web application it doesn't work. Thanks anyway! –  Jinka Feb 10 '12 at 12:11
exporting it to JAR should not change the results, what might be the reason is the reference from where you are executing this jar. Your relative path is from that reference most probably. As your file is not part of the JAR itself (new FileInputStream) –  Johnydep Feb 10 '12 at 12:53
I've tried to put the config.properties in ANY directory that I could think of and it was not working. I don't know why, I guess I did something wrong :/ –  Jinka Feb 10 '12 at 13:06
add comment

Your Answer


