How to Get Environment Variable in Java?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The very common way of getting the Environment variable in Java is by using the System class. The system class provides a method name System.getenv(), which takes an optional String argument and can be used to get the value of an environment variable set in the current system.

The System.getEnv() method is an overloaded method API that returns a String map that contains all the environment variables and their values. We can give a String argument to get the value of the particular value as follows:

The variable has a key-value pair and on passing a key to the System.getEnv() method returns the value of the environment variable.

Another method of getting Environment Variable in Java is by using System Properties. But they have only a limited set of predefined environment variables like java.classpath, java.username, etc.

What is Environment Variable in Java?

An environment variable is a dynamic value used by the operating system and other applications to identify information related to a particular machine. An environment variable is a key value pair. Environment variable values are set outside the program, usually by the operating system through inbuilt functionality.

Various Examples to Get Environment Variables in Java

Let us see a few examples to understand how to get Environment the variable in Java:

Example 1: Getting specific environment variable

Syntax:

The above syntax can be used to store the value of the environment variable in a String for later use. To get the value of the specific variable a String argument is passed.

Let us now see an example to print the classpath for the java program.

Output:

The above example returns values for two variables Class Path and OS.

Example 2: Printing all the Environment Variables

Let us take another example to display all the environment variables of the system.

Syntax:

It returns a map of key-value pairs both being of String type. We can store it in the map for later use. Here we do not pass the String argument.

Output:

As we can see, all the variables are listed. As the optional argument String isn't passed, the System.getenv() method returns a map having key-value pair both of type string. This map contains all the variables present. We are printing them using the System.out.format method to format the output.

Example 3: Getting Environment Variables using System Property

Another method of getting environment variables is using System Property.

Syntax

It is similar to System.getenv() method. The System.getProperty() has values for only selected properties/variable.

Let us use various properties in an example using the System Property of the System class.

Output:

If no such property exists then the system returns null as in the case of home and name. The System.getProperty returns the properties of the specified fields.

Note: Similarly, we can also fetch various properties altogether using System.getProperties.

Learn More

If you found this interesting, you may also like to learn about Java IO Streams

Conclusion

  • To get environment variables we can use either System.getenv() or System.getProperty() methods of the System class.
  • Environment variables are key-value pairs storing system information for the machine used by the operating system and other applications.
  • Methods System.getenv() and System.getProperty() can be used to fetch values of a particular property as well as the list of all the properties.