53

My OS is windows7. I want to read the environment variables in my Java application. I have searched google and many people's answer is to use the method System.getProperty(String name) or System.getenv(String name). But it doesn't seem to work. Through the method, I can read some variable's value that defined in the JVM.

If I set an environment variable named "Config", with value "some config information", how can I get the value in Java?

Matthew
  • 1,905
  • 3
  • 19
  • 26
William
  • 593
  • 1
  • 4
  • 7
  • 3
    `System.getenv()` is the correct call to use. – Greg Kopff Dec 16 '13 at 11:44
  • 5
    possible duplicate of http://stackoverflow.com/questions/461018/how-can-my-java-code-read-os-environment-variables – Linga Dec 16 '13 at 11:47
  • 6
    Also please be aware that if you modify an env variable in windows, you may need to restart your console (or your editor, like IDEA?) for the change to properly kick in. – vikingsteve Dec 16 '13 at 11:48
  • Each process has it's own environment variables. If you set it in one process it will not magically appear or change another. It appears you should be using something else like a Properties file for your setting. – Peter Lawrey Dec 16 '13 at 11:54
  • Instead of voting to close this as duplicate, go and close the linked question as duplicate of this, because this has much better answer. – hyde Dec 16 '13 at 12:11
  • @vikingsteve you are right! i restart my IDE, and then i fixd this problem.Thanks a lot! – William Dec 17 '13 at 15:47
  • @vikingsteve I want to make some friends who love Java programming and good at it. Would you give me a Email Address to communicate with each other about Java, please?:) – William Dec 18 '13 at 13:58
  • @William sure. Not sure how to contact you, but if you add your email in your profile after "Crazy about java" temporarily, like "william at wherever dot com", I'll drop you a note. – vikingsteve Dec 18 '13 at 14:06

2 Answers2

83

You should use System.getenv(), for example:

import java.util.Map;

public class EnvMap {
    public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));
        }
    }
}

When running from an IDE you can define additional environment variable which will be passed to your Java application. For example in IntelliJ IDEA you can add environment variables in the "Environment variables" field of the run configuration.

Notice (as mentioned in the comment by @vikingsteve) that the JVM, like any other Windows executable, system-level changes to the environment variables are only propagated to the process when it is restarted.

For more information take a look at the "Environment Variables" section of the Java tutorial.

System.getProperty(String name) is intended for getting Java system properties which are not environment variables.

Dror Bereznitsky
  • 20,048
  • 3
  • 48
  • 57
69

In case anyone is coming here and wondering how to get a specific environment variable without looping through all of your system variables you can use getenv(String name). It returns "the string value of the variable, or null if the variable is not defined in the system environment".

String myEnv = System.getenv("env_name");
Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
enderland
  • 13,825
  • 17
  • 98
  • 152