Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

IntelliJ IDEA compiles with a JDK that does not know Java 17

ErrorInvalid source release: 17

Short answer

The compiler that IntelliJ or Gradle starts is older than Java 17, but the project asks for Java 17 source. Set the Project SDK to a JDK 17 or newer, make the Gradle JVM the same JDK, and pin a Java toolchain in the build file so every machine compiles with the same version.

  • Applies to IntelliJ IDEA current stable, Gradle 8.x and 9.x, JDK 17 and 21
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which JDK Gradle itself runs on. If it prints a version below 17, the Gradle JVM is the problem; if it prints 17 or higher, look at the IDE project settings.

    ./gradlew --version
  2. 2Match what you see, then jump to its fix

    1. 1The error shows in the IDE build or editor, but ./gradlew build works in a terminalThe Project SDK or language level in the IDE is below 17Fix
    2. 2./gradlew --version shows Java 11 or 8 as the JVMThe Gradle JVM is an older JDKFix
    3. 3It builds on your laptop and fails on a colleague's machine or a CI runner with an older JDKThe build compiles with whatever JDK happens to run GradleFix

What does the error look like?

  • Invalid source release: 17

Cause 1 of 3

The Project SDK or language level in the IDE is below 17

1Check

Project Structure holds the Project SDK and the language level. The level decides which Java constructs the editor accepts, and the bytecode version follows it unless a compiler target is set by hand.

# File > Project Structure > Project > SDK, Language level

2Fix

Pick an installed JDK 17 or newer as Project SDK, then choose language level 17 or higher. Check the module's own language level too, because a module can override the project value.

# File > Project Structure > Project > SDK: 17 or newer; Language level: 17

3Verify

Rebuild the project in the IDE.

# Build > Rebuild Project

Confirmed by jetbrains.com

Did this fix it?

Cause 2 of 3

The Gradle JVM is an older JDK

1Check

For an existing project IntelliJ looks at org.gradle.java.home in gradle.properties first, then JAVA_HOME, then the closest JDK for the Gradle version.

grep -n "org.gradle.java.home" gradle.properties
echo "$JAVA_HOME"

2Fix

Open Settings, then Build, Execution, Deployment, Build Tools, Gradle, and choose a JDK 17 or newer under the Gradle JVM setting. Remove an old org.gradle.java.home line if there is one.

# Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JVM

3Verify

Reload the Gradle project and print the version again.

./gradlew --version

Confirmed by jetbrains.com

Did this fix it?

Cause 3 of 3

The build compiles with whatever JDK happens to run Gradle

1Check

Without a toolchain, Gradle compiles Java with the JDK that runs Gradle, so two machines can give different results.

grep -n -A3 "toolchain" build.gradle build.gradle.kts

2Fix

Declare the Java version in the build file. Gradle then locates a matching JDK for compiling, and downloads one when a toolchain resolver is defined.

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

3Verify

Build again from a clean daemon.

./gradlew --stop
./gradlew build

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Commit the java toolchain block so the Java version is part of the repository, not of each machine.
  • Keep the IDE Gradle JVM and the terminal JAVA_HOME on the same JDK.
  • Run ./gradlew build in CI, not only the IDE build, so a JDK mismatch fails early.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Gradle runs no Cucumber scenarios from my feature files

Feature files exist under src/test but nothing runs

Cucumber runs on the JUnit Platform, so Gradle must call useJUnitPlatform() and have the cucumber-junit-platform-engine on the test classpath.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.