How can I get System variable value in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Java, “system variable” can mean two different things: a JVM system property or an operating-system environment variable. They are not the same mechanism, and many bugs come from mixing them up. The right API depends on which kind of value you are actually trying to read.
Use System.getProperty for JVM System Properties
System properties are key-value pairs exposed by the JVM. They include values such as the Java version, working directory, and user home directory.
These values come from the Java runtime environment. Some are standard properties provided by the JVM. Others can be supplied when you launch the program.
Then in Java:
That is the normal pattern for application-level JVM properties.
Use System.getenv for OS Environment Variables
Environment variables come from the operating system process environment, not the JVM property set.
On Windows, the names may differ, such as USERNAME or USERPROFILE. On Unix-like systems, you often see HOME, PATH, and SHELL.
The key point is that System.getenv reads process environment variables, while System.getProperty reads JVM properties.
They Are Similar in Shape but Different in Meaning
This is the conceptual distinction:
- system properties are Java runtime configuration values,
- environment variables come from the OS process environment.
Both are string-based lookups, which is why they are easy to confuse. But the source, lifecycle, and usual use cases are different.
If you pass -Dname=value to java, that becomes a system property, not an environment variable. If you export NAME=value in the shell before running Java, that becomes an environment variable, not a system property.
Inspecting All Values Can Help Debugging
When diagnosing environment issues, listing everything can be useful.
And for environment variables:
This is helpful during setup debugging, but be careful about secrets. Environment variables often contain credentials or tokens in modern deployments.
Prefer Explicit Configuration Rules
A well-designed application should be clear about which configuration comes from properties and which comes from environment variables. Mixing both everywhere makes deployment harder to reason about.
For example, it is common to use:
- environment variables for deployment and secrets,
- system properties for JVM or launch-time overrides,
- and dedicated config files for larger structured settings.
The exact policy is up to the project, but the distinction should be intentional.
Common Pitfalls
- Using
System.getPropertywhen the value is actually an OS environment variable. - Using
System.getenvfor a value that was passed with-Das a JVM property. - Assuming variable names are portable across operating systems.
- Dumping all environment variables during debugging and accidentally exposing secrets.
- Treating properties and environment variables as interchangeable even though they come from different layers.
Summary
- In Java, read JVM system properties with
System.getProperty(...). - Read operating-system environment variables with
System.getenv(...). - The two mechanisms are similar in API shape but different in source and meaning.
- '
-Dname=valuecreates a system property, not an environment variable.' - Be explicit about where configuration values are expected to come from.
Related reading
- How can I get the current date and time in UTC or GMT in Java?
- How can I get the current stack trace in Java?
- How can I get the latest JRE / JDK as a zip file rather than EXE or MSI installer?
- How can I get the SQL of a PreparedStatement?
- How can I hash a password in Java?
- How can I implement a Runnable with timeout?
- How can I implement static methods on an interface?
- How can I increment a date by one day in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.