How to get the value of Environment variables?
The System class in Java provides a method named System.getenv() which can be used to get the value of an environment variable set in the current system.
Syntax:
public static String getenv(String key);
where key is the Environment variable
whose values we want
Below example illustrates how to use System.getenv() to get the System environment variable:
Example 1: To get the value of a specific environment variable
public class GFG {
public static void main(String[] args)
{
System.out.println(System.getenv( "TEMP" ));
System.out.println(System.getenv( "OS" ));
System.out.println(System.getenv( "JAVA_HOME" ));
}
}
|
Output:

Example 2: To get the value of all environment variables at once
import java.util.Map;
public class GFG {
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));
}
}
}
|
Output:

Note: The output will depend on the system on which you run the above code. A sample output is given above
How to get the value of System Property?
The System class in Java has two methods used to read system properties: