Java
Programming
Environment Variables
Java Options
Software Development

JAVA_OPTS vs JAVA_TOOL_OPTIONS vs _JAVA_OPTIONS: the difference

Codemia Knowledge Hub ·

All three pass JVM arguments through the environment, but at different levels. JAVA_TOOL_OPTIONS is the only one defined by the JVM specification (JVMTI) and is picked up by every compliant JVM. _JAVA_OPTIONS is a HotSpot-specific extension that overrides everything. JAVA_OPTS is not recognized by the JVM at all; it is a convention used by startup scripts like Tomcat's catalina.sh and JBoss's standalone.sh. Picking the wrong one means settings are silently ignored or leak into processes you did not mean to change.

JAVA_TOOL_OPTIONS

Defined in the JVMTI (JVM Tool Interface) specification. Every compliant JVM checks it on startup and prepends its contents to the command-line arguments.

bash
export JAVA_TOOL_OPTIONS="-Xmx512m -Denv=staging"
java -jar myapp.jar
# JVM prints: Picked up JAVA_TOOL_OPTIONS: -Xmx512m -Denv=staging
  • HotSpot prints a Picked up JAVA_TOOL_OPTIONS confirmation to stderr, and there is no flag to suppress it.
  • Its options are prepended, so explicit command-line arguments override them.
  • It affects every Java process started in the environment, including javac, jps, jstack, and build tools (Maven, Gradle) that spawn child JVMs.

_JAVA_OPTIONS

A HotSpot (Oracle/OpenJDK) extension. It is not part of any specification and does not exist in all JVM implementations.

bash
export _JAVA_OPTIONS="-Xmx1024m -XX:+UseG1GC"
java -jar myapp.jar
# JVM prints: Picked up _JAVA_OPTIONS: -Xmx1024m -XX:+UseG1GC
  • Its options are appended to the command line, so they override explicit command-line arguments, which can silently change behavior that was set on purpose.
  • It also prints a stderr pickup message.
  • IBM J9, GraalVM native-image and other non-HotSpot JVMs may not recognize it.

JAVA_OPTS

The JVM never reads this variable. It exists only as a convention in startup scripts.

bash
export JAVA_OPTS="-Xmx1024m -Xms512m"
./catalina.sh start    # Tomcat reads JAVA_OPTS in its script
java -jar myapp.jar    # This process ignores JAVA_OPTS entirely
  • It works only if the script reads it. Tomcat's catalina.sh, WildFly's standalone.sh and some others add $JAVA_OPTS to the java command they build.
  • It has no effect when you run java directly.
  • No pickup message is printed, because the JVM never sees it.

Side-by-side comparison

FeatureJAVA_TOOL_OPTIONS_JAVA_OPTIONSJAVA_OPTS
Defined byJVMTI specificationHotSpot implementationConvention (scripts)
Recognized by JVMYes (all compliant JVMs)Yes (HotSpot only)No
Argument positionPrepended (overridden by CLI args)Appended (overrides CLI args)Depends on script
Prints pickup messageYesYesNo
Affects javac, jps, etc.YesYesNo
Affects direct java -jarYesYesNo
Portable across JVMsYesNo (HotSpot only)N/A

Which value wins when several are set

bash
export JAVA_TOOL_OPTIONS="-Xmx256m"
export _JAVA_OPTIONS="-Xmx1024m"
java -Xmx512m -jar myapp.jar

The effective command line becomes:

 
java -Xmx256m -Xmx512m -Xmx1024m -jar myapp.jar

For -Xmx and most JVM flags the last value wins, so the max heap is 1024m from _JAVA_OPTIONS. That is why _JAVA_OPTIONS is considered dangerous: it silently overrides values set explicitly on the command line.

 
1Priority (last wins for most flags):
21. JAVA_TOOL_OPTIONS  (prepended, lowest priority)
32. Command-line args  (middle)
43. _JAVA_OPTIONS      (appended, highest priority)

Examples

Default heap size for all Java processes

bash
1# Recommended: use JAVA_TOOL_OPTIONS (portable, can be overridden)
2export JAVA_TOOL_OPTIONS="-Xmx512m"
3
4# All Java processes default to 512m, but can be overridden:
5java -Xmx1024m -jar myapp.jar  # uses 1024m (CLI overrides)

Forcing a heap size that cannot be overridden

bash
1# Use _JAVA_OPTIONS to enforce a limit (HotSpot only)
2export _JAVA_OPTIONS="-Xmx512m"
3
4# Even explicit CLI args are overridden:
5java -Xmx2048m -jar myapp.jar  # uses 512m (_JAVA_OPTIONS wins)

Configuring only Tomcat

bash
1# JAVA_OPTS only affects Tomcat (or whichever script reads it)
2export JAVA_OPTS="-Xmx2048m -Xms1024m -XX:+UseG1GC"
3./catalina.sh start
4
5# Other Java processes on the same machine are unaffected
6java -jar another-app.jar  # does not see JAVA_OPTS

Docker containers

Environment variables are the standard way to configure JVM options in containers:

dockerfile
1# Dockerfile
2FROM eclipse-temurin:17-jre
3COPY myapp.jar /app/myapp.jar
4
5# Use JAVA_TOOL_OPTIONS for container-level defaults
6ENV JAVA_TOOL_OPTIONS="-Xmx512m -XX:+UseContainerSupport"
7
8ENTRYPOINT ["java", "-jar", "/app/myapp.jar"]
bash
# Override at runtime with docker run
docker run -e JAVA_TOOL_OPTIONS="-Xmx1024m" myapp

JAVA_TOOL_OPTIONS is the better choice here because it is portable and the orchestrator can override it without changing the Dockerfile.

Kubernetes

yaml
1apiVersion: apps/v1
2kind: Deployment
3spec:
4  template:
5    spec:
6      containers:
7        - name: myapp
8          image: myapp:latest
9          env:
10            - name: JAVA_TOOL_OPTIONS
11              value: "-Xmx768m -XX:MaxRAMPercentage=75.0"
12          resources:
13            limits:
14              memory: "1Gi"

Remote debugging

bash
1# Enable remote debugging for all local Java processes
2export JAVA_TOOL_OPTIONS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
3java -jar myapp.jar
4# Debugger can attach to port 5005

This opens a debug port on every Java process, including mvn and gradle commands.

Which one to use

ScenarioRecommended VariableReason
Default settings for all Java processesJAVA_TOOL_OPTIONSPortable, overridable, spec-compliant
Tomcat/WildFly/JBoss configurationJAVA_OPTSRead by the server's startup script
Force a setting that cannot be overridden_JAVA_OPTIONSAppended last, overrides CLI (use sparingly)
Docker/Kubernetes environmentJAVA_TOOL_OPTIONSClean, portable, works with all JVMs
Debugging a single applicationCLI argumentsNo side effects on other processes
CI/CD pipeline defaultsJAVA_TOOL_OPTIONSAffects build tools and tests uniformly

Mistakes with JVM environment variables

  • Expecting JAVA_OPTS to work with java -jar. The JVM does not read it. JAVA_OPTS="-Xmx1024m" followed by java -jar myapp.jar leaves the heap unchanged. Use JAVA_TOOL_OPTIONS.
  • Setting _JAVA_OPTIONS globally and forgetting it. It overrides CLI arguments, so it can change the behavior of build tools, IDEs and test runners. A global _JAVA_OPTIONS="-Xmx256m" can cause out-of-memory errors in Maven builds that need more heap.
  • Stderr noise. Both JAVA_TOOL_OPTIONS and _JAVA_OPTIONS print a message on every JVM startup, which can trigger false alerts in scripts that parse stderr. It cannot be suppressed.
  • Affecting unintended processes. Setting JAVA_TOOL_OPTIONS in a shell profile means every Java process in that shell inherits it, including javac, jps, keytool, Maven, Gradle and IDE-launched processes. Scope it to one command when possible: JAVA_TOOL_OPTIONS="-Xmx1g" java -jar myapp.jar.
  • Mixing up prepend and append. JAVA_TOOL_OPTIONS is prepended (CLI wins); _JAVA_OPTIONS is appended (it wins).
  • Relying on _JAVA_OPTIONS on non-HotSpot JVMs. IBM Semeru, Azul Zing or GraalVM native images may ignore it entirely. Use JAVA_TOOL_OPTIONS for portability.

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
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms