Gradle has not been officially released for Java 21, but it works sufficiently well with Gradle 8.4 or later for compilation and execution. Essentially, this means that Gradle cannot run on Java 21 LTS just yet. I will briefly demonstrate how I managed to upgrade to Java 21 LTS in my environment using IntelliJ.
Install Java 21 (I’m using Adoptium Temurin) via sdkman.
Pre-requisites:
-
Install Java 21 only on your production system.
-
Use somewhere in your code a command which is unique to the latest Java 21 (in this case.getLast() on a List.
Var testVariable = deputies.getLast();
- Install Java 21 and(!) Java 17 LTS on your development machine.
Changes in your project
“build.gradle” file
Add the toolchain command to your Java section in the “build.gradle” file, as follows:
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
Changes in IntelliJ
I am using IntelliJ IDEA 2023.2.3 (Ultimate Edition).
Ensure that both SDKs are visible in your IDE.
Ensure that the Gradle VM continues to run on Java 17 LTS.
Build your project and deploy
Now, Gradle will continue to run on Java 17 but compile your project using Java 21.
Deploy your binary to your production system, making sure that only Java 21 is installed, and test that any commands exclusive to Java 21 are functioning correctly.