views:

573

answers:

3

Is there any way of specifying the current directory in a java properties file?

i.e. something like:

fileLocation={currentDir}/fileName.txt
+4  A: 

No. Properties files do not have any builtin macro facilities. You can programmatically get the currect directory of the user running the Java app through the user.dir system property.

Michael Borgwardt
Thanks for the answer. Can you set the systems property in this way? For example, if I want to temporarily set the user.dir to some specific directory? I tried changing it before running a unit test, but it still wasn't able to load the file, even though the path returned from getProperty("user.dir") was correct....
Lehane
No, the system property is set when the JVM is started, but later changes to it have no effect. I don't think it's possible to change the working directory of a running JVM.
Michael Borgwardt
@Michael - Thanks.
Lehane
+1  A: 

I'm pretty sure it just defaults to the current directory, if not you can do

fileLocation="./fileName.txt"
cloudhead
+2  A: 

I don't know any direct solution for this problem. You can load the URL to the properties file and then load the filename from that file:

ClassLoader loader = YourClass.class.getClassLoader(); 
URL resourceURL =loader.getResource("check.properties");
String fileToLoad = resourceURL.getPath() + <fileNameFromPropertyFile>;
Develman