48

When trying to configure the project using Java 21 I'm getting the error:

Unsupported Java. Your build is currently configured to use Java 21 and Gradle 8.3.

Unfortunately, there is no information in official documentation for now: https://docs.gradle.org/current/userguide/compatibility.html

A Java version between 8 and 20 is required to execute Gradle. Java 21 and later versions are not yet supported.

Starting from which version does Gradle support Java 21?

6
  • 14
    Current Gradle does not support current Java. That seems to be usual, Gradle tends to lag behind current technologies. Commented Sep 20, 2023 at 8:00
  • 2
    You can watch github.com/gradle/gradle/issues/25574 for the news. Commented Sep 20, 2023 at 8:01
  • 3
    Unless I'm missing something, this doesn't feel like a question that should have been closed (in general, and specifically as seeking recommendations for tools, etc.). It has a current concrete answer (it's not yet supported, but will be with gradle#25574). As well as the related answer that you should still be able to build a Java 21 project with Gradle running under an earlier version of Java using tool java { toolchain { languageVersion = 21 } } Commented Sep 22, 2023 at 17:17
  • 2
    @M.Justin The question is already answered by the quote from the OP: there is currently no version which supports Java 21. There is no real question, so the close is valid. And asking what future version will support Java 21 will either solicit opinion/guesses, or is a request for external resources (e.g. a roadmap). Commented Sep 23, 2023 at 10:33
  • 1
    If you are using Kotlin, then you can change Kotlin version to 1.9.20-Beta2, It's fixed my problem Commented Sep 25, 2023 at 7:17

7 Answers 7

46

Gradle 9.0.0

Update: Gradle 9.0.0 requires Java 17+ to run, uses Kotlin 2 and Groovy 4, and adopts Semantic Versioning (SemVer) with version numbers in the format MAJOR.MINOR.PATCH.

Requiring Java 17+ is a breaking change from previous versions, which supported Java 8+.

See Release Notes, What’s New, and Upgrading to Gradle 9.0.0.

Documentation

See Compatibility Matrix in the fine manual.

screenshot of Gradle Compatibility Matrix from Gradle documentation

Java 22, Gradle 8.7

Gradle 8.7 supports Java 22 for compiling, testing, and running JVM-based projects. See Gradle Release Notes and Gradle User Manual.

But Gradle 8.7 cannot run itself on Java 22. Gradle releases tend to lag behind Java releases because Gradle depends on Groovy and Kotlin which must both be updated first. The workaround is to install both Java 21 and Java 22. Run Gradle 8.7 on Java 21, while letting Gradle manage your Java 22 project. To do this in IntelliJ, see my Answer.

See related Question, Gradle 8.7 cannot find installed JDK 22 in IntelliJ.

Tip: For managing installations of Java versions on Unix-oriented platforms (macOS, Linux, etc.), I like to use SDKMAN!.

Java 21, Gradle 8.5

For full Java 21 support, you can use Gradle 8.5. The release notes say:

With this release, Gradle now fully supports compiling, testing and running on Java 21.


If migrating from older Gradle, see What's new in Gradle 8.0.

Sign up to request clarification or add additional context in comments.

3 Comments

I do like that the release notes call out why Java 21 is not yet supported: "Currently, you cannot run Gradle on Java 21 because Kotlin lacks support for JDK 21. However, support for running Gradle with Java 21 is expected in future versions."
@MJustin to add to that: 1.9.20 (currently preview, scheduled for release ca. Dec 2023) Kotlin releases are already compatible with JVM 21, so I guess that early 2024 is the actual expected date here for Gradle.
This RTFM table gives one version number for what should be a range. There are two kinds of problems, "your Java is too old" and "your Java is too new".
9

Gradle 8.5 is now GA and it has full Java 21 support.

You can update your wrapper config file to:

distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip

And if you are using idea, you can go Settings | Build, Execution, Deployment | Build Tools | Gradle and change the Gradle JVM to 21.

gradle jvm 21

Comments

7

As mentioned in other answers, Gradle 8.4 supports building Java 21 projects.

As to how to do that:

  1. Install JDK21 on the machine and tell Gradle to use it as its toolchain.
    In your Gradle build file (build.gradle or build.gradle.kts):

    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(21)
        }
    }
    

    Gradle should find your JDK21 installation automatically.

  2. Install a JDK version below 21 and use it to execute your Gradle tasks:
    Either point your JAVA_HOME variable to this lower version JDK installation, or point the property org.gradle.java.home (gradle.properties) to it. If you use IntelliJ, you should instead point the setting 'Gradle JVM' to this JDK.

To upgrade Gradle to 8.4, either download it from gradle.org and install it manually or (preferably) update your gradle-wrapper.properties file with distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip.


Explanation: by default, Gradle uses the same JDK that it runs on to compile your sources. Obviously that is not going to work when you need to compile Java 21 code but the latest Gradle release doesn't run on Java 21 yet.
So you let Gradle run on a lower version (either your environment default - what is returned by java --version, or some other JDK as specified for instance in the Gradle JVM option of IntelliJ or by setting org.gradle.java.home in gradle.properties) but you instruct it to use a 21 JDK for building your project (compilation, javadoc generation, etc.).


Edit: Despite claims to the contrary by Gradle, it seems that (at least for simple projects) Gradle 8.4 is already able to run on Java 21.
If you run into problems use the steps above, but it should be enough to simply run gradle build/gradlew build with JDK21 and Gradle 8.4 (so step 2 can be skipped).
Starting with Gradle 8.5 it should be officially supported to run Gradle directly on JDK21.

Comments

5

Gradle Version 8.4 allows you to build with Java 21. Gradle 8.4 itself however can't run on Java 21. The next Version 8.5 is able to run on Java 21.

Gradle 8.5 release notes

Full Java 21 support

Gradle 8.4 supported compiling and testing with Java 21 using Java toolchains, but running Gradle itself on Java 21 was not yet supported.

With this release, Gradle now fully supports compiling, testing and running on Java 21.


Java 22:

Gradle 8.7 can build for Java 22, but needs Java 21 or older to run.

3 Comments

what AGP version supported for that ?
isn't java backwards compatible?
@Rainb Java source and compiled code is generally backwards compatible (i.e. code written and compiled against old versions of Java should run on new versions of Java). This is not that.
3

I believe there is new release of gradle for JDK 21:

https://docs.gradle.org/8.4-rc-1/release-notes.html#kotlin-dsl-improvements

gradle 8.4-rc-1

I was able to install, but I'm still having java errors with classpath issues.

2 Comments

Have you tried a snapshot of Gradle 8.5?
FWIW: I'm running 8.5-rc-3 successfully.
1

If you are using Intellij, until v8.5 is G.A, you need to set Gradle JVM to a JVM version lower than Java 21, example Java 20:

Settings > Build, Execution, Deployment > Build Tools > Gradle:

enter image description here

2 Comments

It's quite a shame that Gradle is taking this long to support JDK 21
8.5 is now available
1

Looking at the Gradle compatability matrix, it will work from Gradle version 8.5.

Java version Support for toolchains Support for running Gradle
20 8.1 8.3
21 8.4 8.5

1 Comment

To be specific, Java 21 projects can be built starting with Gradle 8.4, and Gradle itself can be run under Java 21 starting with Gradle 8.5.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.