Gradle 8.7 cannot find installed JDK 22 in IntelliJ
Asked Answered
N

4

5

I have created a new project with IntelliJ IDEA Ultimate version 2024.1. This version supports the new Java 22 (openjdk-22) version. But when I reload my Gradle project, it shows this error:

Unsupported Gradle JVM.
Your build is currently configured to use Java 22 and Gradle 8.7.
Possible solutions:

- Use Java 21 as Gradle JVM: Open Gradle settings
- Upgrade to Gradle 8.5 and re-sync

AND THIS:

FAILURE: Build failed with an exception.

* What went wrong:
BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 66
> Unsupported class file major version 66

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

CONFIGURE FAILED in 69ms

Under "project structure" I have selected the installed Java 22 SDK. Also, all Modules are of the SDK Default Language Level.

These are my Gradle properties:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

and this is my build.kts:

plugins {
    id("java")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

java {
    sourceCompatibility = JavaVersion.VERSION_22
    targetCompatibility = JavaVersion.VERSION_22

}
dependencies {
    testImplementation(platform("org.junit:junit-bom:5.10.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
    useJUnitPlatform()
}
Nealah answered 31/3 at 16:52 Comment(0)
L
6

Groovy, and therefore Gradle, does not run on Java 22

The problem is not that IntelliJ-IDEA doesn't support Java 22. The problem is that Gradle does not run on Java 22 (it only supports building projects for Java 22 but must be run with Java 21 maximum).

From the Gradle 8.7 release notes:

Support for building projects with Java 22

Gradle now supports using Java 22 for compiling, testing, and starting other Java programs. Selecting a language version is done using toolchains.

You cannot run Gradle 8.7 itself with Java 22 because Groovy still needs to support JDK 22. However, future versions are expected to provide this support.

Lvov answered 31/3 at 18:10 Comment(2)
But that's strange, because it works with another friend. I try to run test classes and it works for him, but not for me. Although we both have the same version of Intellij and Gradle. Once when I built this, I hovered over JJavaVersion.VERSION_22 in build.gradle.kts and it said gradle-8.4.jar. Although the .zip of version 8.4 was in the gradle.properties. It seems to me that Intellji takes a Gradle version from my PC and not the one from the project. I have already deleted the .gradle folder from my Windows user, but it didn't help either.Nealah
Seems 8.5 Gradle version doesn't produce the warning.Livengood
D
4

Answer by Thomas Kläger is correct. Gradle 8.7 can manage a Java 22 project, but itself cannot run on Java 22 because Groovy does not yet run on Java 22.

The next version, Gradle 8.8, does fully support Java 22. Available 2024-05 as a release candidate.

Workaround

The workaround is simple: Install both Java 21 and Java 22.

  • Build & run your project on Java 22.
  • Execute Gradle on Java 21.

IntelliJ is built to support this. A settings panel lets you specify a JVM for Gradle to run itself.

screenshot of IntelliJ settings panel to specify JVM for execution of Gradle

I use SDKMAN! to easily install new JDKs, and uninstall old ones.

================================================================================
Available Java Versions for macOS ARM 64bit
================================================================================
 Vendor        | Use | Version      | Dist    | Status     | Identifier
--------------------------------------------------------------------------------
 Corretto      |     | 22           | amzn    |            | 22-amzn             
               |     | 22.0.1       | amzn    |            | 22.0.1-amzn         
               |     | 21.0.3       | amzn    |            | 21.0.3-amzn         
…       
               |     | 11.0.14.1    | jbr     |            | 11.0.14.1-jbr       
 Liberica      |     | 22.fx        | librca  |            | 22.fx-librca        
               | >>> | 22.0.1.fx    | librca  | installed  | 22.0.1.fx-librca    
               |     | 22.0.1       | librca  |            | 22.0.1-librca       
               |     | 22           | librca  |            | 22-librca           
               |     | 21.0.3.fx    | librca  |            | 21.0.3.fx-librca  
…        
               |     | 11.0.21      | sem     |            | 11.0.21-sem         
 Temurin       |     | 22           | tem     |            | 22-tem              
               |     | 22.0.1       | tem     |            | 22.0.1-tem          
               |     | 21.0.3       | tem     | installed  | 21.0.3-tem          
               |     | 21.0.2       | tem     |            | 21.0.2-tem          
…
               |     | 8.0.392      | zulu    |            | 8.0.392-zulu        
               |     | 8.0.392.fx   | zulu    |            | 8.0.392.fx-zulu     
================================================================================
Drinkwater answered 26/4 at 4:1 Comment(1)
Oh okay, thank you. It go to try itNealah
A
1

I had the same problem, I could fix it, when I changed distrubitionUrl to:

distrubitionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
Asphyxia answered 23/6 at 7:18 Comment(0)
W
0

These solution help me in case if your facing same issue The specified Gradle Distribution xxx does not exists in Intellij go to File -> Settings -> Build -> Build, Execution, Deployment -> Gradle in Gradle user home remove the specified path and Intellij itself will take path pointing .gradle, apply.

Whipstock answered 18/6 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.