Java: invalid source release 21
javac refuses a release newer than its own JDK, and Maven or Gradle wraps the message. Check which JDK compiles, then fix the toolchain, not the flag.

Introduction & Quick Summary
A build fails before compiling any code:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile
Fatal error compiling: error: invalid source release: 21or from plain javac:
error: invalid source release: 21
The compiler was told to target a Java version newer than the JDK doing the work. javac from JDK 17 cannot accept --release 21; it reports the release as invalid, not as unsupported. Maven and Gradle wrap that message, which is why the first line of a failure mentions a plugin and the actual complaint appears a line later.
Where it appears:
- A
pom.xmlwith<maven.compiler.release>21</maven.compiler.release>built on a CI image that ships JDK 17. JAVA_HOMEpointing at an old JDK whilejava -versionin the terminal shows a new one.- Gradle
sourceCompatibility = JavaVersion.VERSION_21with a daemon started under an older JVM. - An IDE using a different SDK from the command line, so the build passes locally and fails on the server.
- Dockerfiles with
FROM maven:3.9-eclipse-temurin-17and a project that already moved to 21.
Quick answer: check the compiler's own version first (javac -version, mvn -version), then either build with a JDK that supports the release or lower the release to the JDK you actually have — and make the toolchain explicit so the two cannot drift again.
Core Mechanics, Signatures & Reproducible Setup
javac has three related flags that are easy to confuse:
--release Ncompiles against the API of Java N and emits class file version N. It is the correct modern flag.-source Nsets the language level only.-target Nsets the class file version only.
Using -source/-target without --release allows code to compile against newer APIs than the target runtime provides, which is how a build succeeds and then throws NoSuchMethodError in production. --release exists since JDK 9 and is what Maven's <release> maps to.
Reproduce the failure in one line, with a JDK 17 and any project:
javac --release 21 Example.java
# error: invalid source release: 21
javac --release 17 Example.java # works
javac -version # javac 17.0.11The same command under JDK 21 compiles the first form and rejects --release 25.
The relationship between release, class file version, and the error you get:
| Release | Class file major | JDK 17 behaviour | JDK 21 behaviour |
|---|---|---|---|
| 8 | 52 | builds | builds |
| 11 | 55 | builds | builds |
| 17 | 61 | builds | builds |
| 21 | 65 | invalid source release | builds |
| 25 | 69 | invalid source release | invalid source release |
Maven configuration that pins the release explicitly:
<properties>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>and the same intent in Gradle:
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}The Gradle toolchain block is different from sourceCompatibility: it makes Gradle find or download a JDK 21 and run the compiler with it, instead of asking the current JVM to pretend.
Key Comparison Table
Which setting decides which JDK does the work:
| Build system | Property | What it controls | Fails how when the JDK is older |
|---|---|---|---|
| Maven | maven.compiler.release | --release flag | invalid source release: N |
| Maven | maven.compiler.source/target | language level and class file only | same message, no API check |
| Maven toolchains | toolchains.xml | which JDK binary runs | No toolchain found |
| Gradle | java.toolchain.languageVersion | JDK used for compile and test | No matching toolchains found |
| Gradle | sourceCompatibility | language level of the current JVM | invalid source release |
javac | --release | API plus class file | invalid source release |
| IDE | Project SDK | editor and its build | editor-only errors |
Diagnosing which JDK is in play, in order:
| Command | What it reveals |
|---|---|
java -version | the runtime on PATH |
javac -version | the compiler on PATH, which may differ |
echo $JAVA_HOME | what Maven and Gradle prefer |
mvn -version | the JDK Maven actually uses, printed as Java version |
./gradlew -version | the daemon's JVM |
mvn help:evaluate -Dexpression=maven.compiler.release -q -DforceStdout | the effective release |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. JAVA_HOME wins over PATH
java -version reports 21 but mvn -version prints Java version: 17.0.11. Maven uses JAVA_HOME when set, and the shell java may come from a different installation:
$ echo "$JAVA_HOME"
/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
$ /usr/libexec/java_home -v 21 # macOS: show the 21 home
$ export JAVA_HOME="$("/usr/libexec/java_home" -v 21)"
$ mvn -version | head -3On Linux, update-alternatives --config java and update-alternatives --config javac are separate switches; changing one leaves the other pointing at the old version.
2. Containers: the image tag is the JDK
maven:3.9-eclipse-temurin-17 contains JDK 17, and no property can change that. Pick an image that matches the release, and let the build fail early if it does not:
FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /src
COPY pom.xml .
RUN mvn -q -B dependency:go-offline
COPY src ./src
RUN mvn -q -B package -DskipTestsMulti-stage builds make the runtime JDK a separate decision: compiling with 21 and running on 21 is the safe default, while compiling with --release 17 and running on 17 is valid if the API surface is respected.
3. Toolchains: make the JDK a declared dependency
Maven can select a JDK independently of the one running Maven. Declare it once and reference it in the build:
<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 ~/.m2/toolchains.xml naming the actual home directory. When the JDK is missing the error becomes Cannot find matching toolchain definitions, which is a far better message than invalid source release.
4. Gradle daemon holding an old JVM
Gradle reuses a daemon between builds. After changing JAVA_HOME, the running daemon keeps the old JVM and the error persists:
./gradlew --stop
./gradlew -version # confirm the JVM
./gradlew buildorg.gradle.java.home in gradle.properties also overrides JAVA_HOME, and a value left over from another machine produces exactly this failure on a fresh checkout.
5. When the message is about the class file instead
A different message with the same root cause:
java.lang.UnsupportedClassVersionError: com/acme/App has been compiled by a more
recent version of the Java Runtime (class file version 65.0), this version of the
Java Runtime only recognizes class file versions up to 61.0This one happens at run time: the build used JDK 21 and production runs JDK 17. Check the class file version of a jar entry rather than guessing:
unzip -p target/app.jar com/acme/App.class | od -An -t u1 -j 6 -N 2The two bytes are the major version (61 for 17, 65 for 21). Fix it by aligning the runtime, not by rebuilding with a lower --release unless the target really is 17.
Common Compilation/Runtime Mistakes & Version Gotchas
Lowering --release to silence the error. If production runs Java 21, compiling for 17 hides the toolchain problem and loses access to newer APIs without telling anyone. Fix the JDK instead of the flag; change the release only when the deployment target is genuinely older.
Mixing source/target with release. Maven passes both when both are configured, and the combination produces confusing errors such as source release 21 requires target release 21. Keep one property — <release> — and delete the pair.
Trusting the IDE's green build. IntelliJ and VS Code compile with their own SDK and their own compiler settings. A project that builds in the IDE and fails in mvn almost always has a different SDK selected in Project Structure; compare mvn -version with the IDE's SDK entry.
Version differences worth knowing. JDK 21 is the current LTS and JDK 25 is the next LTS, so --release 25 on JDK 21 fails exactly like the original example. From JDK 9 onward javac refuses -source 7 and below; JDK 20 removed the ability to target 7 entirely. Preview features require both --release N and --enable-preview, and the class file produced by a preview build is rejected by any other JDK build of the same version — a common cause of UnsupportedClassVersionError during a mixed-version rollout.
Comparing build files. When one module builds and another does not, diff the two pom.xml files with Diff and check the effective values with the JSON viewer after running mvn help:effective-pom -Doutput=effective.xml, which resolves inheritance and profiles that the source file hides.
Reference material: javac manual, --release and Maven compiler plugin, setting the release.
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: DateTimeParseException: Text could not be parsed
The formatter pattern and the input disagree. Check the index in the message, the locale, and whether the text needs a ZonedDateTime instead.
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.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.