Java: invalid target release 21 in Maven and Gradle builds
javac cannot emit the requested release because the build runs on an older JDK. Compare mvn -version with JAVA_HOME, then use release.

Introduction & Quick Summary
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile
(default-compile) on project billing: Fatal error compiling: invalid target release: 21The compiler was asked to emit bytecode for a Java version it cannot produce, because the JDK actually running the build is older than the configured release. java -version in your shell is not evidence: Maven, Gradle and your IDE each pick a JDK independently, and the build picks a different one than the terminal.
The three values that must agree:
- The JDK running the build tool (Maven's JVM, Gradle's daemon JVM).
- The release/source/target configured in the build file.
- The JDK used by the IDE or CI image, which is often a third value again.
| Message | Meaning | Usual owner |
|---|---|---|
invalid target release: 21 | javac cannot emit for 21 | Maven compiler plugin |
invalid source release: 21 | Source level above javac support | Gradle or javac |
invalid flag: --release | javac older than 9 | Ancient JDK |
No compiler is provided in this environment | JRE, not JDK | JAVA_HOME |
class file has wrong version 69.0 | Runtime older than bytecode | Deployment JRE |
Unsupported class file major version 69 | Build tool older than JDK | Gradle/Maven version |
Core Mechanics, Signatures & Reproducible Setup
javac accepts -source, -target and, since JDK 9, --release. --release N selects the documented API surface of version N as well as the class file version, so code cannot accidentally compile against a newer JDK's methods. -source/-target only change syntax and bytecode version, which is how builds succeed locally and fail with NoSuchMethodError in production.
Check what the build tool really uses:
java -version
mvn -version
# Apache Maven 3.9.9
# Java version: 17.0.11, vendor: Eclipse Adoptium
# Java home: /usr/lib/jvm/temurin-17-jdk-amd64
./gradlew --version
# JVM: 17.0.11If Java version is below your configured release, nothing in the POM matters yet.
Maven configuration with a modern compiler plugin:
<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>${maven.compiler.release}</release>
</configuration>
</plugin>
</plugins>
</build>Gradle equivalent, using a toolchain so the daemon JVM does not decide the compiler:
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
tasks.withType<JavaCompile>().configureEach {
options.release = 21
}A reproducible local check that does not depend on shell state:
mvn -q -DskipTests compile -Dmaven.compiler.release=21
./gradlew compileJava --no-daemon -Porg.gradle.java.installations.auto-download=falseKey Comparison Table
| Setting | Controls | API check | Recommended |
|---|---|---|---|
-source 21 -target 21 | Syntax and bytecode | No | Legacy only |
--release 21 | Syntax, bytecode, API | Yes | Yes |
<maven.compiler.release> | Same as --release | Yes | Yes, plugin 3.6+ |
<source>/<target> elements | Legacy pair | No | Avoid |
Gradle options.release | Same as --release | Yes | Yes |
Gradle toolchain | Which JDK compiles | Implied | Yes |
| Where the wrong JDK hides | Command to reveal it | Fix |
|---|---|---|
Shell JAVA_HOME | echo $JAVA_HOME | Export the new JDK |
| Maven runtime | mvn -version | Set JAVA_HOME or ~/.mavenrc |
| Gradle daemon | ./gradlew --version | ./gradlew --stop |
| IDE | Project SDK / Gradle JVM | Set project SDK |
| CI image | docker run ... java -version | Change base image |
| Toolchain file | ~/.m2/toolchains.xml | Point at a real JDK |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
Case 1: Maven runs on an older JDK than the shell
java -version # 21
mvn -version # Java version: 17mvn resolves its JVM from JAVA_HOME first, then PATH. A stale JAVA_HOME in ~/.bashrc or a system-wide profile explains the mismatch:
readlink -f "$(command -v java)"
readlink -f "$JAVA_HOME/bin/java"
export JAVA_HOME=/usr/lib/jvm/temurin-21-jdk-amd64
export PATH="$JAVA_HOME/bin:$PATH"
mvn -versionOn macOS, /usr/libexec/java_home -v 21 prints the right path; on Windows, set JAVA_HOME without a trailing backslash and without quotes, because the Maven wrapper script appends /bin/java literally.
Case 2: Toolchains let one build target several JDKs
<!-- ~/.m2/toolchains.xml -->
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>21</version>
<vendor>temurin</vendor>
</provides>
<configuration>
<jdkHome>/usr/lib/jvm/temurin-21-jdk-amd64</jdkHome>
</configuration>
</toolchain>
</toolchains><plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals><goal>toolchain</goal></goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk><version>21</version></jdk>
</toolchains>
</configuration>
</plugin>With a toolchain, Maven can run on JDK 17 and compile with JDK 21, which is the supported way to build for a newer release than the one the build tool runs on.
Case 3: Gradle daemon keeps the old JVM
After installing a new JDK, Gradle reuses the running daemon, so ./gradlew build still compiles with the old one:
./gradlew --stop
./gradlew --version
./gradlew compileJavaIf the toolchain is declared but no matching JDK exists, Gradle fails with No matching toolchains found for requested specification: {languageVersion=21}. Either install the JDK or enable provisioning explicitly:
# gradle.properties
org.gradle.java.installations.auto-download=trueAuto-download requires the Foojay resolver plugin in settings.gradle.kts; without it the property is ignored and the same message returns.
Case 4: CI image with a JRE instead of a JDK
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE
rather than a JDK?# wrong for builds
FROM eclipse-temurin:21-jre
# right
FROM eclipse-temurin:21-jdkThe JRE image has no javac and no tools.jar equivalent. Keep the JRE base for the runtime stage of a multi-stage build and the JDK base for the build stage.
Case 5: Android and Kotlin modules in the same Gradle build
An Android module pinned to Java 17 and a new module pinned to 21 fail together with invalid source release when the Android Gradle Plugin does not support 21 bytecode at that version. Align the modules rather than raising one:
subprojects {
plugins.withId("org.jetbrains.kotlin.jvm") {
extensions.configure<JavaPluginExtension> {
toolchain.languageVersion = JavaLanguageVersion.of(17)
}
}
}Also set kotlinOptions.jvmTarget (or the compiler options DSL in Kotlin 2.x) to the same version; a Kotlin jvmTarget of 21 with a Java toolchain of 17 produces a mismatch error in the other direction.
Case 6: --release refuses to lower to an old version with newer APIs
javac --release 8 Main.java
# error: cannot find symbol (java.util.List.of)--release 8 uses the JDK 8 API signatures, so methods added later do not exist during compilation. That is the desired protection. If the target must stay at 8 while using newer conveniences, either vendor a backport or raise the target; a -source/-target pair would hide the problem and fail at runtime with NoSuchMethodError.
Common Compilation/Runtime Mistakes & Version Gotchas
- Trusting
java -version. Build tools, IDEs and CI each resolve a JDK separately. Verify withmvn -versionand./gradlew --versionbefore editing any POM. - Trailing slash in
JAVA_HOME. On Windows,C:\Program Files\Java\jdk-21\produces...jdk-21\\bin\java, which some launchers resolve incorrectly, and the build falls back toPATH. sourceandtargetwithoutrelease. The build succeeds on a newer JDK and fails at runtime on the server's older JRE withNoSuchMethodErrororUnsupportedClassVersionError.- Maven compiler plugin versions.
maven.compiler.releaseis honoured from plugin 3.6.0; on 3.5.1 it is ignored and the build silently compiles with the default source level. - Java 25 class files. Bytecode major version 69 requires a matching runtime; a
21deployment plus a25build producesclass file has wrong version 69.0at startup, not at compile time. - IDE cache after a JDK switch. Maven projects in IntelliJ keep an imported JDK per module; changing
JAVA_HOMEalone leaves the module compiling with the old one until the project is reimported.
To check what bytecode a published jar actually contains, inspect its class version and compare against the target with the hash generator for artifact identity and the diff tool for manifest differences. For environment values that reach the build, format them into a readable list with the env file viewer.
Primary references: maven-compiler-plugin compile documents release, source and target, and Gradle toolchains explains how the compiler JDK is selected independently of the daemon JVM.
Official references
primary documentationThe Java documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesSpring Boot: No qualifying bean of type available
The context could not satisfy a constructor argument. Scan boundaries, missing stereotypes, ambiguous candidates and test slices are the usual reasons.
Method Scope in Java: How Local Variables Work
Understand how variables behave inside Java methods: local variable boundaries, block scope, parameter scope, shadowing, and lifetime implications.
Java: java.net.BindException: Address already in use
The kernel refused the listening socket. Find the process holding the port, check the bound interface and privileged ports, then restart cleanly.
Java: No suitable driver found for jdbc: URL
DriverManager found no driver accepting the URL. Put the driver on the runtime classpath, match the URL prefix and handle split classloaders.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.