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. Overview

We often need to pass JVM options without modifying our scripts when configuring Java applications. Instead of manually adding flags every time we run the java command, we can use the environment variables JDK_JAVA_OPTIONS or JAVA_TOOL_OPTIONS. Both environment variables serve the same purpose—passing JVM options dynamically—but they work in different ways.

In this tutorial, we’ll explore their differences, when to use each, and best practices for managing JVM configurations effectively.

2. What Are JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS?

These two environment variables both allow us to specify JVM options globally, eliminating the need to modify the options every time we execute JDK tools such as java, javac, javadoc, etc.

JAVA_TOOL_OPTIONS was introduced in Java 5. On the other hand, JDK_JAVA_OPTIONS variable is more recent, introduced in Java 9. Their behavior and purpose are quite different.

Before we dive into how each variable functions, let’s create a simple Java source file:

package com.baeldung;
/**
 * A simple class to print some variables' value
 */
public class TestEnvVar {

    public static void main (String[] args){
        System.out.println("var1 = '" + System.getProperty("var1") + "'");
        System.out.println("var2 = '" + System.getProperty("var2") + "'");
    }

}

The code above is pretty simple. Our main() method prints out the value of var1 and var2. Later, we’ll pass this argument to this class using these two environment variables.

Next, let’s compile the code:

$ javac com/baeldung/TestEnvVar.java

Now, we have the following file structure:

$ tree
.
└── com
    └── baeldung
        ├── TestEnvVar.class
        └── TestEnvVar.java

3 directories, 1 file

By the way, we’ll use Java 23 to compile or run all examples on Linux in this tutorial:

$ java -version
openjdk version "23.0.1" 2024-10-15
OpenJDK Runtime Environment Homebrew (build 23.0.1)
OpenJDK 64-Bit Server VM Homebrew (build 23.0.1, mixed mode, sharing)

Next, let’s run the class and pass some values to the required variables.

3. Executing the Java Launcher – the java Command

A typical way to start a Java program is using the Java launcher – the java command.

First, let’s define var1 and var2 in JDK_JAVA_OPTIONS and start our main() method:

$ JDK_JAVA_OPTIONS="-Dvar1='Hello (JDK_JAVA_OPTIONS)' -Dvar2='World (JDK_JAVA_OPTIONS)'" java com.baeldung.TestEnvVar
NOTE: Picked up JDK_JAVA_OPTIONS: -Dvar1='Hello (JDK_JAVA_OPTIONS)' -Dvar2='World (JDK_JAVA_OPTIONS)'
var1 = 'Hello (JDK_JAVA_OPTIONS)'
var2 = 'World (JDK_JAVA_OPTIONS)'

As the output indicates, the java command reads the JDK_JAVA_OPTIONS variable and passes the values of var1 and var2 to the main() method, producing the expected result. Also, java outputs a message to inform us that JDK_JAVA_OPTIONS was picked up.

It’s important to note that we set the environment variable and executed the java command on the same line, separated by a space. This ensures that JDK_JAVA_OPTIONS is only set for that specific java command. If we check the variable after running the command, we’ll see that JDK_JAVA_OPTIONS no longer holds a value:

$ echo $JDK_JAVA_OPTIONS
   

Now, let’s set var1 and var2 in JAVA_TOOL_OPTIONS and run our class:

$ JAVA_TOOL_OPTIONS="-Dvar1='Hi (JAVA_TOOL_OPTIONS)' -Dvar2='There (JAVA_TOOL_OPTIONS)'" java com.baeldung.TestEnvVar
Picked up JAVA_TOOL_OPTIONS: -Dvar1='Hi (JAVA_TOOL_OPTIONS)' -Dvar2='There (JAVA_TOOL_OPTIONS)'
var1 = 'Hi (JAVA_TOOL_OPTIONS)'
var2 = 'There (JAVA_TOOL_OPTIONS)'

Similarly, the JAVA_TOOL_OPTIONS variable affects the Java launcher. Also, a similar message tells us the java command read JAVA_TOOL_OPTIONS.

Now, some might wonder: what happens if the same variable is set in both JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS? Which one takes precedence? Next, let’s test it:

$ JAVA_TOOL_OPTIONS="-Dvar1='Hi (JAVA_TOOL_OPTIONS)' -Dvar2='There (JAVA_TOOL_OPTIONS)'" JDK_JAVA_OPTIONS="-Dvar2='World (by JDK_JAVA_OPTIONS)'" java com.baeldung.TestEnvVar
NOTE: Picked up JDK_JAVA_OPTIONS: -Dvar2='World (by JDK_JAVA_OPTIONS)'
Picked up JAVA_TOOL_OPTIONS: -Dvar1='Hi (JAVA_TOOL_OPTIONS)' -Dvar2='There (JAVA_TOOL_OPTIONS)'
var1 = 'Hi (JAVA_TOOL_OPTIONS)'
var2 = 'World (by JDK_JAVA_OPTIONS)'

In this example, we defined var1 and var2 in JAVA_TOOL_OPTIONS, but we only defined var2 with a different value in JDK_JAVA_OPTIONS. The output shows java picked up both environment variables. But if the same variables are set in both JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS, JDK_JAVA_OPTIONS takes precedence.

4. Other Java Commands

Apart from the java command, JDK provides several other commands that allow us to compile, document, and manage Java applications efficiently, such as:

  • javac – the Java compiler
  • javadoc – the JavaDoc generator
  • and many more

Next, let’s figure out how these tools handle JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS.

We’ll take the javac compiler as an example. Unlike the java command, checking variable values through javac isn’t straightforward. Therefore, to test its behavior, we’ll set the Max Heap Size (-Xmx) JVM parameter in JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS, and verify whether it affects javac as expected. 

First, let’s check the default MaxHeapSize value javac takes:

$ javac -J-XX:+PrintCommandLineFlags com/baeldung/TestEnvVar.java
... -XX:MaxHeapSize=9663676416 ...

In this example, the -J-XX:+PrintCommandLineFlags option is used with certain JDK tools, javac in this case, to print out the command-line flags that are being used by the JVM when the tool starts.

As the output shows, on this machine, JVM takes 9GiB (9*1024*1024*1024=9663676416 Bytes) memory as the default MaxHeapSize. Next, let’s set 10MiB (10*1024*1024 = 10485760 bytes) as MaxHeapSize in JAVA_TOOL_OPTIONS and compile the source file again:

$ JAVA_TOOL_OPTIONS="-Xmx10m" javac -J-XX:+PrintCommandLineFlags com/baeldung/TestEnvVar.java
Picked up JAVA_TOOL_OPTIONS: -Xmx10m
... -XX:MaxHeapSize=10485760 ...

As the output shows, JAVA_TOOL_OPTIONS affects javac.

Next, let’s do the same test with the JDK_JAVA_OPTIONS variable:

$ JDK_JAVA_OPTIONS="-Xmx10m" javac -J-XX:+PrintCommandLineFlags com/baeldung/TestEnvVar.java
... -XX:MaxHeapSize=9663676416 ...

It turns out that JDK_JAVA_OPTIONS doesn’t affect javac. It compiles the source file still using the default option: MaxHeapSize=9GiB.

Actually, this isn’t just javac. It applies to all JDK tools except the java command—only JAVA_TOOL_OPTIONS is recognized.

Next, let’s verify it using another JDK tool, the javadoc command:

$ JAVA_TOOL_OPTIONS="-Xmx10m" javadoc -J-XX:+PrintCommandLineFlags com/baeldung/TestEnvVar.java
Picked up JAVA_TOOL_OPTIONS: -Xmx10m
... -XX:MaxHeapSize=10485760 ...

$ JDK_JAVA_OPTIONS="-Xmx10m" javadoc -J-XX:+PrintCommandLineFlags com/baeldung/TestEnvVar.java | sed 's/ /\n/g' | grep MaxHeapSize
Loading source file com/baeldung/TestEnvVar.java...
... -XX:MaxHeapSize=9663676416 ...

As we can see, the command ignores the -Xmx10m parameter set in JDK_JAVA_OPTIONS, but correctly recognizes the value from the JAVA_TOOL_OPTIONS variable.

5. JDK_JAVA_OPTIONS vs. JAVA_TOOL_OPTIONS

So far, we discussed how these two environment variables work with JDK tools. Next, to have a clear overview, let’s summarize the differences in a table:

JAVA_TOOL_OPTIONS JDK_JAVA_OPTIONS
Purpose Environment variable to pass JVM options and arguments to JDK tools Environment variable to pass JVM options and arguments to the Java launcher to start a Java application
Scope It affects all JDK tools (java, javac, javadoc, jar, etc.) It affects only the java command
Works In Java 5+ Java 9+
Precedence for the Java launcher The values in JDK_JAVA_OPTIONS overwrites the same options set in this variable. Takes precedence.

Since the JDK_JAVA_OPTIONS environment variable was introduced in Java 9 and is exclusive to the java command, it should be used when setting options for launching applications on Java 9 or later. However, if we need to set options globally across JDK tools, JAVA_TOOL_OPTIONS is the appropriate choice.

6. Conclusion

In this article, we’ve explored the differences between the JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS with practical examples.

We’ve also learned when to use each variable, helping us manage JVM options effectively across different JDK tools.

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.

eBook Jackson – NPI EA – 3 (cat = Jackson)
4 Comments
Oldest
Newest
Inline Feedbacks
View all comments