How to get an environment variable in Java?

Posted on In QA

In Java, how to get the value (string) of an environment variable?

You may call the System.getenv(name) library function in Java to get the environment variable value.

public static String getenv(String name)

Parameters:
name – the name of the environment variable

Returns:
the string value of the variable, or null if the variable is not defined in the system environment

One example, in the Java REPL:

java> System.getenv("JAVA_HOME")
java.lang.String res0 = "/usr/java/default/"

Leave a Reply

Your email address will not be published. Required fields are marked *