eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

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.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – LS – NPI (cat=Java)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)