How to get the environment variables in Java
In Java, the System.getenv() returns an unmodifiable string Map
view of the current system environment.
Map<String, String> env = System.getenv();
// get PATH environment variable
String path = System.getenv("PATH");
Table of contents.
- 1. Get a specified environment variable
- 2. UnsupportedOperationException
- 3. Display all environment variables
- 4. Sort the environment variables
- 5. Download Source Code
- 6. References
1. Get a specified environment variable
1.1 Below example uses System.getenv
to get the JAVA_HOME
environment variable.
JavaExample1.java
package com.mkyong.core;
public class JavaExample1 {
public static void main(String[] args) {
String java_home = System.getenv("JAVA_HOME");
System.out.println(java_home);
}
}
Output
Terminal
/usr/local/Cellar/openjdk@11/11.0.10/libexec/openjdk.jdk/Contents/Home
1.2 If a requested environment variable is not defined, the System.getenv
will return a null
.
String java_home = System.getenv("JAVA_HOME");
if (java_home == null) {
System.err.println("JAVA_HOME environment variable is not defined!");
} else {
System.out.println(java_home);
}
1.3 Java 8, Optional
example to handle the null
.
String JAVA_HOME =
Optional.ofNullable(System.getenv("JAVA_HOME1"))
.orElseThrow(
() -> new IllegalArgumentException("Please defined JAVA_HOME environment variable."));
2. UnsupportedOperationException
The System.getenv()
returns an unmodifiable string Map
, modify the Map
will cause UnsupportedOperationException
.
JavaExample2.java
package com.mkyong.core;
import java.util.Map;
public class JavaExample2 {
public static void main(String[] args) {
Map<String, String> env = System.getenv();
// throws UnsupportedOperationException
env.put("CUSTOM_JAVA_HOME", "/opt/java99/");
}
}
Output
Terminal
Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.Collections$UnmodifiableMap.put(Collections.java:1505)
at com.mkyong.twilio.DisplayEnvironment.main(DisplayEnvironment.java:10)
3. Display all environment variables
The below example will print out all the available system environment variables.
JavaExample3.java
package com.mkyong.core;
import java.util.Map;
public class JavaExample3 {
public static void main(String[] args) {
Map<String, String> env = System.getenv();
// Java 8
env.forEach((k, v) -> System.out.println(k + ":" + v));
// Classic way to loop a map
/*for (Map.Entry<String, String> entry : env.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}*/
}
}
Output
Terminal
PATH:/usr/local/opt/node@14/bin:/usr/local/opt/openjdk@17/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
SHELL:/bin/zsh
JAVA_HOME:/usr/local/Cellar/openjdk@11/11.0.10/libexec/openjdk.jdk/Contents/Home
COMMAND_MODE:unix2003
TMPDIR:/var/folders/vx/b9b5sdsn3191wg30hkw_lbwh0000gn/T/
#...
4. Sort the environment variables
The below example will sort and display the environment variables in alphabetical order.
JavaExample4.java
package com.mkyong.core;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class JavaExample4 {
public static void main(String[] args) {
Map<String, String> env = System.getenv();
LinkedHashMap<String, String> collect =
env.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldValue, newValue) -> oldValue,
LinkedHashMap::new)
);
collect.forEach((k, v) -> System.out.println(k + ":" + v));
}
}
Output
Terminal
HOME:/Users/yongmookkim
JAVA_HOME:/usr/local/Cellar/openjdk@11/11.0.10/libexec/openjdk.jdk/Contents/Home
JAVA_MAIN_CLASS_37972:com.mkyong.twilio.DisplayEnvironment
LC_CTYPE:en_MY.UTF-8
PATH:/usr/local/opt/node@14/bin:/usr/local/opt/openjdk@17/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
SHELL:/bin/zsh
#...
5. Download Source Code
$ git clone https://github.com/mkyong/core-java
6. References
About Author
Comments
Subscribe
0 Comments