Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readJava
#java#maven#gradle#toolchain
Bold white headline reading WRONG JDK with a Java compiler crate rejecting an amber release card on the right.

Introduction & Quick Summary

A build fails before compiling any code:

code
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile Fatal error compiling: error: invalid source release: 21

or 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.xml with <maven.compiler.release>21</maven.compiler.release> built on a CI image that ships JDK 17.
  • JAVA_HOME pointing at an old JDK while java -version in the terminal shows a new one.
  • Gradle sourceCompatibility = JavaVersion.VERSION_21 with 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-17 and 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 N compiles against the API of Java N and emits class file version N. It is the correct modern flag.
  • -source N sets the language level only.
  • -target N sets 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:

bash
javac --release 21 Example.java # error: invalid source release: 21 javac --release 17 Example.java # works javac -version # javac 17.0.11

The 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:

ReleaseClass file majorJDK 17 behaviourJDK 21 behaviour
852buildsbuilds
1155buildsbuilds
1761buildsbuilds
2165invalid source releasebuilds
2569invalid source releaseinvalid source release

Maven configuration that pins the release explicitly:

xml
<properties> <maven.compiler.release>21</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>

and the same intent in Gradle:

kotlin
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 systemPropertyWhat it controlsFails how when the JDK is older
Mavenmaven.compiler.release--release flaginvalid source release: N
Mavenmaven.compiler.source/targetlanguage level and class file onlysame message, no API check
Maven toolchainstoolchains.xmlwhich JDK binary runsNo toolchain found
Gradlejava.toolchain.languageVersionJDK used for compile and testNo matching toolchains found
GradlesourceCompatibilitylanguage level of the current JVMinvalid source release
javac--releaseAPI plus class fileinvalid source release
IDEProject SDKeditor and its buildeditor-only errors

Diagnosing which JDK is in play, in order:

CommandWhat it reveals
java -versionthe runtime on PATH
javac -versionthe compiler on PATH, which may differ
echo $JAVA_HOMEwhat Maven and Gradle prefer
mvn -versionthe JDK Maven actually uses, printed as Java version
./gradlew -versionthe daemon's JVM
mvn help:evaluate -Dexpression=maven.compiler.release -q -DforceStdoutthe 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:

bash
$ 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 -3

On 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:

dockerfile
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 -DskipTests

Multi-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:

xml
<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:

bash
./gradlew --stop ./gradlew -version # confirm the JVM ./gradlew build

org.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:

code
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.0

This 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:

bash
unzip -p target/app.jar com/acme/App.class | od -An -t u1 -j 6 -N 2

The 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 documentation

The Java documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.