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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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:

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.