Regarding application.properties file and environment variable

The Apache Commons project has expanded handling of properties files that lets you use environment variables (in particular, see the Variable Interpolation section). Then you should be able to get what you want using:

pathToInputFile=${env:TOM_DATA}/data/incoming/ready/

Tom Duckering's answer is correct. Java doesn't handle this for you.

Here's some code utilizing regular expressions that I wrote to handle environment variable substitution:

/*
 * Returns input string with environment variable references expanded, e.g. $SOME_VAR or ${SOME_VAR}
 */
private String resolveEnvVars(String input)
{
    if (null == input)
    {
        return null;
    }
    // match ${ENV_VAR_NAME} or $ENV_VAR_NAME
    Pattern p = Pattern.compile("\\$\\{(\\w+)\\}|\\$(\\w+)");
    Matcher m = p.matcher(input); // get a matcher object
    StringBuffer sb = new StringBuffer();
    while(m.find()){
        String envVarName = null == m.group(1) ? m.group(2) : m.group(1);
        String envVarValue = System.getenv(envVarName);
        m.appendReplacement(sb, null == envVarValue ? "" : envVarValue);
    }
    m.appendTail(sb);
    return sb.toString();
}

That is right. Java does not handle substituting the value of the environment variables. Also Java might not recognise variables like $EXT_DIR. While using such variables you might encounter FileNotFoundException. But Java will recognise the variables that are defined with -D in catalina.sh. What I mean by this, is suppose you have such a definition in catalina.sh

CATALINA_OPTS="-Dweb.external.dir="$EXT_DIR"

In your properties file use ${web.external.dir} instead of using *$EXT_DIR*. And while accessing this property in your code you could do it this way:

String webExtDir = System.getProperty("web.external.dir");

Hope this will help a lot of people so they won't have to pick bits and pieces from everywhere which takes really long to resolve an issue at hand.


You can put environment variables in your properties file, but Java will not automatically recognise them as environment variables and therefore will not resolve them.

In order to do this you will have to parse the values and resolve any environment variables you find.

You can get at environment variables from Java using various methods. For example: Map<String, String> env = System.getenv();

There's a basic tutorial here: http://java.sun.com/docs/books/tutorial/essential/environment/env.html

Hope that's of some help.