Java
System Variables
Environment Variables
Programming
Java Tips

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.

Browse interview questions

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.

java
1public class Main {
2    public static void main(String[] args) {
3        System.out.println(System.getProperty("java.version"));
4        System.out.println(System.getProperty("user.home"));
5        System.out.println(System.getProperty("user.dir"));
6    }
7}

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.

bash
java -Dapp.mode=dev Main

Then in Java:

java
String mode = System.getProperty("app.mode");
System.out.println(mode);

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.

java
1public class Main {
2    public static void main(String[] args) {
3        String path = System.getenv("PATH");
4        String home = System.getenv("HOME");
5
6        System.out.println(path);
7        System.out.println(home);
8    }
9}

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.

java
System.getProperties().forEach((key, value) ->
    System.out.println(key + " = " + value)
);

And for environment variables:

java
System.getenv().forEach((key, value) ->
    System.out.println(key + " = " + value)
);

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.getProperty when the value is actually an OS environment variable.
  • Using System.getenv for a value that was passed with -D as 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=value creates a system property, not an environment variable.'
  • Be explicit about where configuration values are expected to come from.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.