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