Let's get started with a Microservice Architecture with Spring Cloud:
Java System.getProperty vs System.getenv
Last updated: July 25, 2025
1. Introduction
In Java, System.getProperty() and System.getenv() are both used to retrieve configuration values, but they serve different purposes and access different sources of information.
In this tutorial, let’s break down their differences, use cases, and typical examples.
2. Using System.getProperty()
The Java platform uses a Properties object to provide information about the local system and configuration. We call it system properties.
System properties are typically used to configure JVM-specific parameters, such as file encoding, user directories, JVM version, and other Java-specific configurations. We can use System.getProperty() to read a property value by its name.
Next, let’s use System.getProperty() to read the value of a few standard operating-system-related properties:
String osArch = System.getProperty("os.arch");
String osName = System.getProperty("os.name");
String osVersion = System.getProperty("os.version");
String fileSep = System.getProperty("file.separator");
log.info("operating system name: {}", osName);
log.info("operating system arch: {}", osArch);
log.info("Operation System version: {}", osVersion);
log.info("file separator: {}", fileSep);
Running this test on our current system (Arch Linux), we’ll see this output:
[main] INFO com...SystemPropertyAndEnvUnitTest -- operating system name: Linux
[main] INFO com...SystemPropertyAndEnvUnitTest -- operating system arch: amd64
[main] INFO com...SystemPropertyAndEnvUnitTest -- Operation System version: 6.6.34-1-lts
[main] INFO com...SystemPropertyAndEnvUnitTest -- file separator: /
If we run the same test on a macOS system, we’ll get a different output:
[main] INFO com...SystemPropertyAndEnvUnitTest -- operating system name: Mac OS X
[main] INFO com...SystemPropertyAndEnvUnitTest -- operating system arch: aarch64
[main] INFO com...SystemPropertyAndEnvUnitTest -- Operation System version: 15.0
[main] INFO com...SystemPropertyAndEnvUnitTest -- file separator: /
We can also use the System.getProperties() method to get a complete list of properties. Next, let’s print them out:
System.getProperties().forEach((k, v) -> LOG.info("{} -> {}", k, v));
When we execute this line, we’ll see a long list in the output:
sun.arch.data.model -> 64
user.timezone -> Europe/Berlin
java.vm.specification.version -> 22
os.name -> Linux
sun.java.launcher -> SUN_STANDARD
user.country -> US
...
java.vm.version -> 22.0.2
sun.io.unicode.encoding -> UnicodeBig
java.class.version -> 66.0
We can update a property value at runtime or introduce our own new properties using the System.setProperty() method:
System.setProperty("nice.tech.site", "Baeldung");
assertEquals("Baeldung", System.getProperty("nice.tech.site"));
In the above example, we created a new property nice.tech.site with the value “Baeldung“.
Alternatively, we can also pass our properties to the application using the java command’s -D command-line argument:
java -jar app.jar -DpropertyName=value
For example, we can start our app and enable the nice.tech.site property using this command:
java -jar app.jar -Dnice.tech.site="Baeldung"
It’s worth noting that System.getProperty() always returns a String.
3. Using System.getenv()
Environment variables are operating-system-level key/value pairs. They’re often used to configure application-level or system-level settings, such as paths to binaries or external services. Environment variables are typically set globally outside of the JVM.
The method of setting an environment variable differs from one operating system to another. For example, in Windows, we use a System Utility application from the control panel, while in Unix-like systems, we usually use shell scripts.
When creating a process, it inherits a clone environment of its parent process by default. In Java, we can use System.getProperty() to retrieve environment variables from the underlying operating system:
String homeDir = System.getenv("HOME");
String shell = System.getenv("SHELL");
String terminal = System.getenv("TERM");
log.info("User Home: {}", homeDir);
log.info("Shell: {}", shell);
log.info("Terminal: {}", terminal);
When we run this code, we can see the environment variables’ values in the output:
[main] INFO com...SystemPropertyAndEnvUnitTest -- User Home: /home/kent
[main] INFO com...SystemPropertyAndEnvUnitTest -- Shell: /bin/zsh
[main] INFO com...SystemPropertyAndEnvUnitTest -- Terminal: xterm-256color
We can also get all environment variables as a Map if we don’t pass a variable name to System.getenv():
System.getenv().forEach((k, v) -> LOG.info("{} -> {}", k, v));
We get the following output when executing the above line:
COLORTERM=truecolor
COMMAND_MODE=unix2003
HOME=/home/kent
LC_CTYPE=UTF-8
...
LOGNAME=U533276
PATH=/home/kent/bin;/user/bin;...
PYTHON2_BIN=/home/kent/.pyenv/shims
SHELL=/bin/zsh
It’s important to note that System.getenv() returns a read-only Map. Trying to add values to the Map raises UnsupportedOperationException:
Map<String, String> sysEnv = System.getenv();
assertThrows(UnsupportedOperationException.class, () -> sysEnv.put("TECH_SITE", "Baeldung"));
Usually, a Java application doesn’t set environment variables at runtime. However, if this is required, there are a few ways to achieve it, such as by using the Reflection API, although it isn’t as straightforward as setting system properties.
We can also use the ProcessBuilder API to set environment variables for a new process. Next, let’s see an example:
ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "echo $TECH_SITE");
Map<String, String> env = pb.environment();
env.put("TECH_SITE", "Baeldung");
try (BufferedReader output = new BufferedReader(new InputStreamReader(pb.start().getInputStream()))) {
String result = output.readLine();
log.info("TECH_SITE in the new process: {}", result);
}
log.info("TECH_SITE in the current process: {}", System.getenv("TECH_SITE"));
If we run this code, we get this output:
[main] INFO com...SystemPropertyAndEnvUnitTest -- TECH_SITE in the new process: Baeldung
[main] INFO com...SystemPropertyAndEnvUnitTest -- TECH_SITE in the current process: null
In this example, we set the environment variable TECH_SITE=Baeldung for the new process (/bin/sh—c echo $TECH_SITE). As the output shows, the TECH_SITE variable is only available in the new process, not in the current process.
4. The Differences
Now, let’s summarize the key differences between system properties and environment variables:
| System properties | Environment variables | |
|---|---|---|
| Scope | JVM specific | Operating system level |
| Creation | Can be created in JVM settings, via -Dkey=value command-line arguments, or at runtime using System.setProperty() | Variables are mainly defined in operating systems, such as during system initialization or through configuration scripts.They can also be defined programmatically when starting new processes, such as using ProcessBuilder API. |
| Mutability | Can be modified at runtime by System.setProperty() | Read-only variables, cannot be modified at runtime |
Understanding the differences between system properties and environment variables is helpful for us in choosing the right method for our Java application.
5. Conclusion
Both System.getProperty() and System.getenv() are essential methods for accessing system-related information in Java, but they serve different purposes.
System.getProperty() deals with JVM-specific properties, while System.getenv() allows us to access environment variables at the operating system level.
Understanding when to use each method can help us write more flexible, reliable, and environment-aware Java applications.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.

















