Menu

Gradle Toolchain vs JAVA_HOME for Java 21 Builds
📰

Gradle Toolchain vs JAVA_HOME for Java 21 Builds

DEV Community: java·Noah·about 18 hours ago
#ET4KJZR3
#devtojava#devto#dev#gradle#toolchain
Long article (2661 words) — no AI summary yet.
Reading 0:00
15s threshold

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

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.