Get value of System property or environment variable in Java
This post will discuss how to get the value of a System property or an environment variable in Java.
1. Using System.getenv() method
To get the value of all environment variables, we can use the System.getenv() method, which returns a map containing all the key-value pairs of environment variables. Then we can iterate over the Map, and print the keys and values. For example, to print all the environment variables and their values, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Map; class Main { public static void main(String[] args) { Map<String, String> env = System.getenv(); for (String envName: env.keySet()) { System.out.println(envName + ":" + env.get(envName)); } } } |
To get the value of a specific environment variable, we can directly call the overloaded System.getenv(String) method, It accepts the name of the environment variable and returns the string value of the variable, or null if the variable is not defined in the system environment. For example, to get the value of the JAVA_HOME variable, which represents the installation directory of Java, we can write:
|
1 2 3 4 5 6 7 8 9 10 |
import java.util.Map; class Main { public static void main(String[] args) { String path = System.getenv("JAVA_HOME"); System.out.println(path); } } |
2. Using System.getProperty() method
To get the value of all system properties, we can use the System.getProperties() method, which returns a Properties object containing all the key-value pairs of system properties. Then we can iterate over the Properties class and print the keys and values. For example, to print all the system properties and their values, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Properties; class Main { public static void main(String[] args) { Properties props = System.getProperties(); for (String key : props.stringPropertyNames()) { System.out.println(key + ":" + props.getProperty(key)); } } } |
To get the value of a specific system property, we can directly call the overloaded System.getProperty(String) method, which returns the value of the property with the given key, or null if the property is not found. For example, to get the value of the os.name property, which represents the current operating system, we can write:
|
1 2 3 4 5 6 7 8 |
class Main { public static void main(String[] args) { String name = System.getProperty("os.name"); System.out.println(name); } } |
These are some of the ways to get the value of system property or environment variable in Java. We can also use other methods or classes to manipulate or access these values, such as System.setProperty(String key, String value) to set a system property, or System.getenv(String name, String def) to get an environment variable with a default value if it is not defined.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)