CodeGym /Java Blog /Core Java /Get Environment variables in Java
Author
Vasyl Malik
Senior Java Developer at CodeGym

Get Environment variables in Java

Published in the Core Java group

What are Environment Variables?

Every computer programming language contains variables and constants which allocate unique memory locations, containing information to be used by the programs. Constant values are not changeable but the variable’s value can be changed. Environment variables are the key/value pair whose value is set outside the program and their reference is always available to the program at any time. Both key and value are strings. The conventions for setting and using environment variables always differ between operating systems and command line interpreters. They are always globally available to every application running on the system.

Example


PATH = "C:\\WINDOWS\system32;"
Here, Path is the environment variable set outside the program but available to each program running on windows.

Use Case

As we know every change in the program needs to be executed or re-deployed to the server which may produce unwanted side effects in production. So the main purpose of introducing environment variables is to restrict this execution and deployment again and again.

How To Get Environment Variables in Java?

Let’s see how to get environment variables in java. Java provides us with 2 ways to get environment variables in the Java program.
  1. System.getenv()

  2. System.getProperty()

Now let’s see both of them in detail one by one.

System.getenv()

The System.getenv() method is used to fetch all the environment variables but if a specific key name is provided as a parameter then it will fetch its value. java.lang.System.getenv() always returns the string map of all the available environment variables.

Declaration


public static String getenv(String name)
Here, name is the parameter used to specify the key against which we needed the value.

Return

It will return the value of the key or null if the provided parameter is not associated with any key. If the parameter is not passed then it will return all the available key/value pairs.

Example


import java.util.Map;
public class SystemGetEnvDemo {

   public static void main(String[] args) {

      // getting value for environment variable "PATH"
      System.out.print("System.getenv(PATH) = ");
      System.out.println(System.getenv("PATH"));

      // getting value for environment variable "TEMP" resulting in null
      System.out.print("System.getenv(TEMP) = ");
      System.out.println(System.getenv("TEMP"));
      
      //getting all environment variables using System.getenv()
      Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));
        }
   }
}

Output

System.getenv(PATH) = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin System.getenv(TEMP) = null PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PROGRAMIZ_COMPILER_SERVICE_HOST=10.0.10.151 KUBERNETES_PORT=tcp://10.0.0.1:443 PROGRAMIZ_COMPILER_PORT=tcp://10.0.10.151:80 TERM=xterm PROGRAMIZ_COMPILER_WEB_UI_SEVICE_PORT_80_TCP_PROTO=tcp KUBERNETES_SERVICE_HOST=10.0.0.1 PS1= PROGRAMIZ_COMPILER_WEB_UI_SEVICE_PORT_80_TCP_PORT=80 PROGRAMIZ_COMPILER_WEB_UI_SEVICE_PORT_80_TCP_ADDR=10.0.14.233 PROGRAMIZ_COMPILER_PORT_80_TCP=tcp://10.0.10.151:80 PROGRAMIZ_COMPILER_PORT_80_TCP_PROTO=tcp PWD=/app KUBERNETES_PORT_443_TCP=tcp://10.0.0.1:443 PROGRAMIZ_COMPILER_PORT_80_TCP_ADDR=10.0.10.151 PROGRAMIZ_COMPILER_WEB_UI_SEVICE_PORT=tcp://10.0.14.233:80 KUBERNETES_PORT_443_TCP_ADDR=10.0.0.1 PROGRAMIZ_COMPILER_WEB_UI_SEVICE_SERVICE_HOST=10.0.14.233 PROGRAMIZ_COMPILER_PORT_80_TCP_PORT=80 KUBERNETES_PORT_443_TCP_PROTO=tcp KUBERNETES_SERVICE_PORT=443 PROGRAMIZ_COMPILER_SERVICE_PORT=80 PROGRAMIZ_COMPILER_WEB_UI_SEVICE_PORT_80_TCP=tcp://10.0.14.233:80 PROGRAMIZ_COMPILER_WEB_UI_SEVICE_SERVICE_PORT=80 HOSTNAME=programiz-compiler-deployment-58bfd77477-dtlq8 KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_SERVICE_PORT_HTTPS=443 HOME=/home/compiler
You may get a different output depending on your operating system.

System.getProperty()

We can also use java.lang.System.getProperty() in Java to retrieve the value for the specified key passed as a parameter to this method. This method is used to retrieve the system properties, i.e information about the local system and configurations. If the environment variable is present as a system property in java then it’s better to use System.getProperty() for getting value in a platform-independent way. The value for properties can be changed at the runtime but not the case with environment variables.

Declaration


public String getProperty(String name)
Here, name is the parameter used to specify the key against which we needed the value.

Return

It will return the value of the key or null.

Example


import java.lang.*; 
import java.util.Properties; 

public class SystemGetPropertyDemo { 
    public static void main(String[] args) 
    { 
        // getting username system property 
       // using System.getProperty in Java
        System.out.println("user.name: " +  System.getProperty("user.name")); 
        // getting property with key home resulting in null
        // calling system.getproperty()
        System.out.println("home: " + System.getProperty("home")); 
        // getting name of Operating System 
        System.out.println("os.name: " + System.getProperty("os.name")); 
    }
}

Output

user.name: compiler home: null os.name: Linux

Conclusion

We hope by now you understand what are and how to get environment variables in java, their purpose, and how to use methods to get them. Feel free to practice and get back whenever you need more assistance. Happy learning!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION