IntelliJ IDEA compiles with a JDK that does not know Java 17
Invalid source release: 17Short 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.
Fix it fast
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 --version2Match what you see, then jump to its fix
- 1
The 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
./gradlew --version shows Java 11 or 8 as the JVMThe Gradle JVM is an older JDKFix - 3
It 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
- 1
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 level2Fix
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: 173Verify
Rebuild the project in the IDE.
# Build > Rebuild ProjectConfirmed by jetbrains.com
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 JVM3Verify
Reload the Gradle project and print the version again.
./gradlew --versionConfirmed by jetbrains.com
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.kts2Fix
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 buildConfirmed by developer.android.com
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
A Gradle build fails on classes that use Lombok annotations
Error: Annotation processors must be explicitly declared now.
Lombok is two things for Gradle: a compile-time library and an annotation processor.
3 causes, each with a fixBack end
Gradle 8 fails on Report destination: switch to outputLocation
What went wrong: 'java.io.File org.gradle.api.reporting.ConfigurableReport.getDestination()'
Gradle 8.0 removed the deprecated destination and enabled properties of the Report type.
2 causes, each with a fixBack end
A scanner flags jackson-databind: how to upgrade it in Gradle
jackson-databind vulnerability reported by a dependency scanner
Find where the old jackson-databind comes from, then raise it.
3 causes, each with a fixBack end
Gradle cannot resolve spring-boot-gradle-plugin after a Spring Boot 3 upgrade
Plugin [id: 'org.springframework.boot', version: '3.0.1'] was not found in any of the following sources
Spring Boot 3 needs Java 17 or newer and a recent Gradle, and its Gradle plugin is published on the Gradle Plugin Portal.
3 causes, each with a fix