Java System getenv() Method
Example
Print the value of the PATH environment variable, and of one that does not exist:
import java.util.Map;
public class Main {
public static void main(String[] args) {
System.out.println(System.getenv("PATH"));
System.out.println(System.getenv("MY_COLOR"));
Map<String, String> env = System.getenv();
System.out.println(env.containsKey("PATH"));
}
}
Note: The output depends on the computer and its settings.
Definition and Usage
The getenv() method returns the value of an environment variable (a named value that the operating system gives to every program, like PATH). If the variable does not exist, it returns null.
Without a name, getenv() returns all environment variables as a Map, where the keys are the names and the values are the values. You can read the map, but you cannot change it.
Syntax
One of the following:
public static String getenv(String name)
public static Map<String, String> getenv()
Parameter Values
| Parameter | Description |
|---|---|
| name | Optional. The name of the environment variable. Without it, the method returns a map of all environment variables. |
Technical Details
| Returns: | A String with the value of the variable, or null if it does not exist.Without a name: a Map<String, String> with all environment variables. The map cannot be changed. |
|---|
Related Pages
❮ System Methods