Your shell says JAVA_HOME points to JDK 21, but ./gradlew build still produces Java 17 bytecode. That is not a contradiction: JAVA_HOME and a Gradle toolchain control different JVMs, and one build can use three or four of them at once.
This guide maps the layers that choose a JDK, shows how to find the one that is winning, and gives a setup that compiles, tests, and runs on Java 21 in both Gradle and Maven.
Table of Contents
- The Five Places a JVM Build Picks a JDK
- Toolchain vs JAVA_HOME and Which One Wins
- Why Your Build Still Compiles With JDK 17
- Configure a Java 21 Toolchain
- Mirror the Setup in CI and Maven
- Decision Table
- Conclusion
The Five Places a JVM Build Picks a JDK
Gradle's documentation on toolchains for JVM projects lists five mechanisms that influence the Java version used by a project: Java toolchains, the --release flag, source and target compatibility, the JAVA_HOME environment variable, and IDE settings. They are not mutually exclusive, which is why a project can show one version in the IDE, another in gradle --version, and a third in the compiled class files.
| Layer | What it decides | Where it is set |
|---|---|---|
| Gradle client JVM | Which JVM starts gradlew and contacts the daemon |
JAVA_HOME, PATH, or the IDE |
| Gradle daemon JVM | Which JVM executes the build and tasks that do not fork a tool |
JAVA_HOME, org.gradle.java.home, daemon JVM criteria, or the Tooling API |
| Project toolchain | Which JDK compiles, tests, and generates Javadoc |
java.toolchain in the build |
| Compiler options | Language level, target bytecode, and available APIs |
options.release, sourceCompatibility, targetCompatibility
|
| IDE Gradle JVM | Which JVM the IDE uses to launch Gradle, and the fallback when no toolchain exists | IntelliJ Gradle JVM or Eclipse Gradle JDK |
Two of the layers in the table are processes rather than project settings. When you run ./gradlew, the wrapper starts a client JVM. The client connects to a daemon, and the daemon executes the build. The Gradle daemon documentation explains the client and daemon JVMs separately: the client takes its JVM from JAVA_HOME, PATH, or the IDE, while the daemon uses JAVA_HOME by default but can be overridden.
Compile, test, and Javadoc tasks are different again. They can fork tools from a project toolchain. A Java toolchain is the set of javac, java, and javadoc binaries for a language version, discovered from installed JDKs or downloaded by Gradle. If a project declares one, it takes over those tasks.
Toolchain vs JAVA_HOME and Which One Wins
The short answer: if the build declares a Java 21 toolchain, JAVA_HOME does not decide what compiles the code. If the build declares no toolchain, the daemon's JDK does by default. JAVA_HOME is only the default source for the client and the daemon.
Several precedence rules explain the surprising cases:
-
Daemon selection: a
gradle/gradle-daemon-jvm.propertiesfile generated byupdateDaemonJvmtakes precedence over bothJAVA_HOMEandorg.gradle.java.home. Otherwise,org.gradle.java.homewins overJAVA_HOME. - Toolchain selection: among installations that match the requested language version, Gradle prefers the JVM already running Gradle, then JDKs over JREs, then vendor order (Adoptium first), then higher versions. The language version is a major version, so any 21.x can satisfy a request for 21; adding a vendor narrows the candidates but not the patch level.
-
Explicit build settings: a toolchain in the build overrides the daemon for compile, test, and Javadoc tasks. That cuts both ways:
JAVA_HOME=21cannot change a build that asks for a 17 toolchain.
The differences are easier to see in one table:
| Mechanism | Selects the JDK? | Sets bytecode target? | Blocks newer APIs? | Can download a JDK? |
|---|---|---|---|---|
| Java toolchain | Yes | Only through the compiler default | No | Yes, with a resolver |
options.release |
No | Yes | Yes | No |
sourceCompatibility / targetCompatibility
|
No | Yes | No | No |
JAVA_HOME |
Client and default daemon | No | No | No |
org.gradle.java.home |
Daemon | No | No | No |
| Daemon JVM criteria | Daemon | No | No | Yes, from configured URLs |
| IDE Gradle JVM | Gradle process inside the IDE | No | No | No |
Java 21 also raises the Gradle version floor. The Gradle compatibility matrix shows that toolchains can compile and test with Java 21 from Gradle 8.4, but running Gradle itself on Java 21 requires Gradle 8.5 or later. A current Gradle 9.x release needs a JVM between 17 and 27, so it can run on 17 and still drive a 21 compiler through a toolchain.
Two different mistakes produce the same "still on 17" symptom:
-
The build asks for 17. A
toolchain { languageVersion = 17 }, a convention plugin, orsourceCompatibility = 17keeps the language level at 17.JAVA_HOME=21cannot override an explicit toolchain, andsourceCompatibilitycannot raise the target above the compiler. -
The build asks for nothing. Compile and test then use the daemon JVM. If the IDE starts Gradle with its bundled 17 JBR, or
org.gradle.java.homepoints at 17, or daemon criteria pin 17, the daemon is 17 even though your shell'sJAVA_HOMEis 21.
options.release is a third trap. It chooses the API and bytecode level, not the compiler:
tasks.withType<JavaCompile>().configureEach {
options.release = 21
}
Setting options.release = 21 when the compiler is JDK 17 makes javac fail with an unsupported release; it does not fetch JDK 21. Use a toolchain to choose the JDK and release only to set the target.
Why Your Build Still Compiles With JDK 17
Run these checks in the same environment that produces the bad build: the same shell, the same CI job, or the same IDE terminal. A shell that exports 21 proves nothing about an IDE that launches Gradle with its own JVM.
1. Compare the launcher and the daemon
./gradlew --version
On Gradle 8.9 and later, the output separates the two JVMs, as described in the Gradle 8.9 release notes:
Launcher JVM: 21.0.13 (Eclipse Adoptium 21.0.13+11)
Daemon JVM: /usr/lib/jvm/temurin-17
Launcher JVM is what started the wrapper. Daemon JVM is what actually runs the build. If the launcher is 21 and the daemon is 17, check both layers: the build's toolchain decides compilation, while the daemon decides tasks that have no toolchain. Your shell's JAVA_HOME is not the explanation. A single JVM: line means the wrapper predates 8.9; upgrade it before chasing settings.
2. List the detected toolchains
./gradlew -q javaToolchains
The report shows whether auto-detection and auto-download are enabled, and it lists every installation Gradle can see. Each entry includes a Detected by line, which tells you whether it came from JAVA_HOME, SDKMAN, a Maven toolchain file, or a custom path. If there is no Java 21 entry, no toolchain setting can select one: install JDK 21 or configure a download repository.
3. Inspect the daemon
./gradlew buildEnvironment
buildEnvironment reports the daemon JVM with its location, language version, vendor, and whether it is a full JDK. Use it when --version is ambiguous or when you need the exact installation path.
4. Find explicit settings in the build
Search the build for toolchain, sourceCompatibility, targetCompatibility, options.release, Kotlin's jvmToolchain, and Android's compileOptions. Check convention plugins and included builds, not only the root build file.
Then check configuration files:
-
gradle.propertiesin the project and in~/.gradle/gradle.propertiesfororg.gradle.java.home - Any
org.gradle.java.installations.paths,org.gradle.java.installations.fromEnv, or auto-detect settings -
gradle/gradle-daemon-jvm.propertiesfor a pinned daemon version
5. Check the IDE
In IntelliJ IDEA, the Gradle JVM is under Settings > Build, Execution, Deployment > Gradle. In Eclipse, it is under Preferences > Gradle > Gradle JDK. This setting controls the Gradle process inside the IDE, not the project toolchain. Align it with the command-line JVM if you want one daemon, or leave it different intentionally.
6. Stop the old daemon
./gradlew --stop
The daemon caches toolchain paths and configuration. Gradle's documentation recommends a restart because changes may only take effect for a new daemon.
7. Prove which JDK compiled and tested
The info log names the toolchain:
./gradlew compileJava --rerun-tasks --info | grep "Compiling with toolchain"
The log line contains the installation path, for example Compiling with toolchain '/opt/jdk-21'. If the build has no toolchain, the log says Compiling with JDK Java compiler API, and the compiler is the daemon's JDK.
You can also inspect the class file with javap from JDK 21 or newer. The Java Virtual Machine Specification lists the class-file major versions: Java 21 is major version 65, and Java 17 is 61.
javap -verbose build/classes/java/main/com/example/App.class | grep major
major version: 65
That output proves the bytecode target, not necessarily the compiler. If you compile with JDK 21 but set options.release = 17, the class file is 61 even though javac 21 produced it. Pair the class-file check with the Compiling with toolchain log when you need to prove the compiler itself.
The test task uses the project toolchain by default. If a test fails with UnsupportedClassVersionError, its JVM is older than the test classes; give the test task the same toolchain or an explicit Java 21 launcher.
Configure a Java 21 Toolchain
Declare the toolchain in every project that compiles Java, usually through a convention plugin in multi-project builds:
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
The Groovy DSL uses the same property assignment. This configures compileJava, compileTestJava, test, and javadoc to use a Java 21 installation. If you need a specific distribution, add vendor = JvmVendorSpec.ADOPTIUM or another recognized vendor. Because languageVersion selects only a major version, any 21.x installation can match; pin the vendor and control the build image if the patch level matters.
If a machine has no JDK 21 yet, Gradle can download one, but only when a toolchain download repository is configured. Add the Foojay resolver convention plugin to settings.gradle.kts:
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0"
}
Gradle installs the downloaded JDK under the Gradle User Home and reuses it. Only generally available releases are downloaded; early-access builds need a local installation. If your network blocks downloads, set org.gradle.java.installations.auto-download=false and point org.gradle.java.installations.paths at your JDKs.
Auto-download is convenient, but locked-down CI and offline machines often disable it. In that case, make sure the host actually has a JDK 21. If it does not, follow the steps to install JDK 21 for the build to target, then rerun ./gradlew -q javaToolchains to confirm that Gradle detects it.
If you also want the build logic to run on Java 21, pin the daemon:
./gradlew updateDaemonJvm --jvm-version=21
This writes gradle/gradle-daemon-jvm.properties. Commit the file so developers and CI get the same daemon; it outranks JAVA_HOME and org.gradle.java.home. By default, the task needs toolchain download repositories to generate platform URLs. You do not need it just to compile with a 21 toolchain: a Gradle 8.5 or later daemon on 17 can run the build while the toolchain supplies javac 21.
For an exception, such as one test suite that must run on a different JDK from the rest of the project, wire a task-level launcher:
tasks.register<Test>("testsOn21") {
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(21)
}
}
The standard test task already follows the project toolchain, so task-level configuration is only for exceptions.
Mirror the Setup in CI and Maven
Gradle in CI
- Export
JAVA_HOMEin the same step that runs./gradlew, and use a JDK that the wrapper supports. - Log
./gradlew --versionand./gradlew -q javaToolchainsso a failed job shows which JVM it used. - Commit the wrapper and, if used,
gradle/gradle-daemon-jvm.properties; do not rely on the CI image's default Java. - If toolchain auto-download is enabled, allow network access to the resolver. Otherwise pre-install JDK 21 and disable downloads.
Maven parity
Maven has the same split. Maven itself runs on the JDK selected by JAVA_HOME or by java on PATH, and mvn -v prints that runtime, as the Maven installation guide describes.
maven.compiler.release is Maven's version of --release. It sets the API and bytecode target but does not choose the compiler JDK. If Maven runs on 17, a release of 21 fails just as it does in Gradle. The Maven Compiler Plugin guide to targeting a Java version also notes that source and target do not prevent accidental use of newer APIs.
Maven's toolchain equivalent is toolchains.xml plus maven-toolchains-plugin. The compiler, Surefire, Failsafe, and Javadoc plugins are toolchain-aware, so they use the selected JDK even when Maven runs on another one. The Maven toolchains guide shows the two required pieces.
In pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>[21,22)</version>
</jdk>
</toolchains>
</configuration>
</plugin>
In ~/.m2/toolchains.xml:
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>21.0.13</version>
<vendor>temurin</vendor>
</provides>
<configuration>
<jdkHome>/opt/jdk-21</jdkHome>
</configuration>
</toolchain>
</toolchains>
Use a range such as [21,22) in the plugin configuration. Maven matches a single version exactly, so <version>21</version> will not match a toolchains.xml entry that says 21.0.13. Since version 3.2.0, the toolchains plugin can also discover installed JDKs and generate the file instead of requiring you to write it by hand.
Gradle's auto-detection also reads Maven toolchain specifications, so a shared ~/.m2/toolchains.xml can serve both builds.
Decision Table
| Scenario | Recommended setup | Why it works | Main pitfall |
|---|---|---|---|
| One Java 21 for build logic, compiler, and tests | JDK 21 via JAVA_HOME, a 21 toolchain, and a Gradle 8.5+ wrapper |
Client, daemon, and tasks agree | Gradle 8.4 or older cannot run on Java 21 |
| Daemon on 17, compile and test on 21 | A 21 toolchain only; Gradle 8.4 or later, preferably 8.5+ | The toolchain forks Java 21 tools while the daemon stays on a supported JVM | Expecting JAVA_HOME=21 to change the compiler |
| Mixed modules on 17 and 21 | Per-project or per-subproject toolchains; task-level launchers for exceptions | Each project declares its own target | One global JAVA_HOME cannot serve both |
| Build on 21 but ship Java 17 bytecode | A 21 toolchain plus options.release = 17
|
The toolchain picks javac 21; release limits the API and bytecode |
sourceCompatibility 17 without release can still link newer APIs |
| Legacy build that cannot adopt toolchains |
JAVA_HOME or org.gradle.java.home pointing at the one JDK |
Gradle uses that JDK everywhere | A global setting cannot vary by project |
| Maven project | Toolchain plugin plus toolchains.xml; maven.compiler.release for the target |
It mirrors Gradle's split between JDK and target | A single version string such as 21 not matching 21.0.13
|
Conclusion
In a modern Gradle build, JAVA_HOME is not the compiler selector. It picks the client JVM and the default daemon, while a project toolchain picks the compiler, test launcher, and Javadoc tool. Daemon JVM criteria sit above both, and --release or sourceCompatibility changes only the target, not the JDK.
To diagnose a build that still uses 17, compare Launcher JVM and Daemon JVM, list installations with javaToolchains, inspect the build and gradle.properties for explicit 17 settings, and stop the daemon after changes. Then prove the result with the Compiling with toolchain log and the class-file major version.
For Java 21, the durable setup is a 21 toolchain in the build, a Gradle 8.5 or later wrapper, and a JDK 21 available to developers and CI. Pin the daemon to 21 only if build logic needs it. Maven mirrors the same idea with a toolchain for the JDK and release for the target.
Top comments (0)