I have a test package which is based on Selenium and the configuration is in a test.properties file. Now all the config values are hard coded there. I would like to hide some config values to hide from the version control and would like to put it as env variables. But how can read the env variables inside test.properties file and assign values? Will the -D option work in this case?

  • Are you talking about java?
    – Alexey R.
    Sep 1, 2017 at 9:48

2 Answers 2


I hope -D option will help to some extent. But directly java won't interpret the environment variables. You need to handle it. The below link have more insight on implementation of the similar option. https://stackoverflow.com/questions/2263929/regarding-application-properties-file-and-environment-variable

  1. I assume you're talking about java
  2. You wouldn't be able to achieve your goal without writing some extra code. Below is the example of how you can to that:

Example pre-conditions:

  • your property file is called "test.properties"

  • the property that you want to take is called "somepropname"

  • environement variable you want to take propertyvalue from is "some-prop-env-name"

It is also worth mentioning that in such the appoach you can either:

  • set a property in a property file and thus it will be picked from the file

  • do not set a property in a file. If it will not present in a file it will be picked from environment variable

The example itself:

public static void main(String args[]) {

    Properties prop = new Properties();
    final String SOME_PROP_ENV_NAME = "some-prop-env-name";

    try {
        prop.load(new FileReader("test.properties"));
        String propValue = prop.getProperty("somepropname", System.getenv().get(SOME_PROP_ENV_NAME));
    } catch (IOException e) {


What's been basically done is using getProperty(propName, defaultValue) method where defaultValue is taken from an environment variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.