- Environment Variables
- Querying Environment Variables
- Passing Environment Variables to New Processes
- Platform Dependency Issues
- How to Get Environment Variable in Java?
- What is Environment Variable in Java?
- Various Examples to Get Environment Variables in Java
- Example 1: Getting specific environment variable
- Example 2: Printing all the Environment Variables
- Example 3: Getting Environment Variables using System Property
- Learn More
- Conclusion
Environment Variables
Many operating systems use environment variables to pass configuration information to applications. Like properties in the Java platform, environment variables are key/value pairs, where both the key and the value are strings. The conventions for setting and using environment variables vary between operating systems, and also between command line interpreters. To learn how to pass environment variables to applications on your system, refer to your system documentation.
Querying Environment Variables
On the Java platform, an application uses System.getenv to retrieve environment variable values. Without an argument, getenv returns a read-only instance of java.util.Map , where the map keys are the environment variable names, and the map values are the environment variable values. This is demonstrated in the EnvMap example:
import java.util.Map; public class EnvMap < public static void main (String[] args) < Mapenv = System.getenv(); for (String envName : env.keySet()) < System.out.format("%s=%s%n", envName, env.get(envName)); >> >
With a String argument, getenv returns the value of the specified variable. If the variable is not defined, getenv returns null . The Env example uses getenv this way to query specific environment variables, specified on the command line:
Passing Environment Variables to New Processes
When a Java application uses a ProcessBuilder object to create a new process, the default set of environment variables passed to the new process is the same set provided to the application’s virtual machine process. The application can change this set using ProcessBuilder.environment .
Platform Dependency Issues
There are many subtle differences between the way environment variables are implemented on different systems. For example, Windows ignores case in environment variable names, while UNIX does not. The way environment variables are used also varies. For example, Windows provides the user name in an environment variable called USERNAME , while UNIX implementations might provide the user name in USER , LOGNAME , or both.
To maximize portability, never refer to an environment variable when the same value is available in a system property. For example, if the operating system provides a user name, it will always be available in the system property user.name .
How to Get Environment Variable in Java?
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
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.
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.
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.
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.
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.
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.