How to get the value of System Property and Environment Variable in Java?

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
// Java program to get the value// of a specific environment variable// using System.getenv() method  public class GFG {    public static void main(String[] args)    {          // Get the value of        // the TEMP environment variable        System.out.println(System.getenv("TEMP"));          // Get the value of        // the OS environment variable        System.out.println(System.getenv("OS"));          // Get the value of        // the JAVA_HOME environment variable        System.out.println(System.getenv("JAVA_HOME"));    }} |
Output:
Example 2: To get the value of all environment variables at once
// Java program to get the value// of all environment variables at once// using System.getenv() method  import java.util.Map;  public class GFG {    public static void main(String[] args)    {          // Get the value of        // all environment variables at once        // and store it in Map        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:
- java.lang.System.getProperty(String key): fetches only those properties – values that you will specify using the key(associated to that particular value that you want).
Example:
// Java Program illustrating the working// of getProperty(String key) methodÂÂimportjava.lang.*;importjava.util.Properties;ÂÂpublicclassNewClass {   Âpublicstaticvoidmain(String[] args)   Â{       Â// Printing Name of the system property       ÂSystem.out.println("user.dir: "                          Â+ System.getProperty(                                Â"user.dir"));       Â// Fetches the property set with 'home' key       ÂSystem.out.println("home: "                          Â+ System.getProperty(                                Â"home"));       Â// Resulting in Null as no property is present       Â// Printing 'name of Operating System'       ÂSystem.out.println("os.name: "                          Â+ System.getProperty(                                Â"os.name"));       Â// Printing 'JAVA Runtime version'       ÂSystem.out.println("version: "                          Â+ System.getProperty(                                Â"java.runtime.version"));       Â// Printing 'name' property       ÂSystem.out.println("name: "                          Â+ System.getProperty(                                Â"name"));       Â// Resulting in Null as no property is present   Â}}Output:
user.dir: /tmp/hsperfdata_bot home: null os.name: Linux version: 1.8.0_101-b13 name: null
- java.lang.System.getProperty(String key, String definition): helps you to create your own key-value sets that you want.
Example:
// Java Program illustrating the working of// getProperty(String key, String definition) methodÂÂimportjava.lang.*;importjava.util.Properties;ÂÂpublicclassNewClass {   Âpublicstaticvoidmain(String[] args)   Â{       Â// Here key = "Hello" and       Â// System Property = "Geeks"       ÂSystem.out.println("Hello property : "                          Â+ System.getProperty(                                Â"Hello","Geeks"));       Â// Here key = "Geek" and       Â// System Property = "For Geeks"       ÂSystem.out.println("System-property :"                          Â+ System.getProperty(                                Â"System","For Geeks"));       Â// Here key = "Property" and       Â// System Property = null       ÂSystem.out.println("Property-property :"                          Â+ System.getProperty(                                Â"Property"));   Â}}Output:
Hello key property : Geeks System key property :For Geeks Property key property :null
- java.lang.System.getProperties(): fetches all the properties – values that the JVM on your System gets from the Operating System.
Example:
// Java Program illustrating the working of// getProperties() methodÂÂpublicclassGFG {Â Â Â Âpublicstaticvoidmain(String[] args)Â Â Â Â{ÂÂÂ Â Â Â Â Â Â ÂSystem.out.println(System.getProperties())Â Â Â Â}}Output:




