How to get an environment variable in Java?
Posted on In QAIn 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 variableReturns:
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/"