views:

33

answers:

2

I want to define a property for a working directory(say work.dir=/home/username/working-directory), for my production .properties file, without hard-coding the /home/username.I want to reference the system property user.home in place on the hard-coded /home/username, to make work.dir more generic. How do I reference the system property and concatenate it will other user-defined strings in a user-defined .properties? Note: I don't want to access the user.home property in my java code, but from the .properties that I have defined. I want to be able to replace the value of the work.dir with different value for both my production and development(JUnit tests for example).

A: 

while generating properties file you will have to fetch system property and write it. like

System.getproperty("user.home");
org.life.java
I guess my question is not clear. I have a file called file.properties for example. In the file.properties, I have defined work.dir=/home/username/working-directory. I want to use user.home in the file.properties file so that I can remove /home/username.
walters
A: 

Get the property from the file, then replace supported macros.

String propertyValue = System.getProperty("work.dir");
String userHome = System.getProperty("user.home" );
String evaluatedPropertyValue = propertyValue.replace("$user.home", userHome );
Andy Thomas-Cramer