Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readJava
#Java#Maven#Gradle#Build Tools
A Java compiler gate rejecting a cyan release dial, an amber JAVA_HOME card and a toolchain crate below

Introduction & Quick Summary

text
[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: 21

The 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.
MessageMeaningUsual owner
invalid target release: 21javac cannot emit for 21Maven compiler plugin
invalid source release: 21Source level above javac supportGradle or javac
invalid flag: --releasejavac older than 9Ancient JDK
No compiler is provided in this environmentJRE, not JDKJAVA_HOME
class file has wrong version 69.0Runtime older than bytecodeDeployment JRE
Unsupported class file major version 69Build tool older than JDKGradle/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:

bash
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.11

If Java version is below your configured release, nothing in the POM matters yet.

Maven configuration with a modern compiler plugin:

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

kotlin
java { toolchain { languageVersion = JavaLanguageVersion.of(21) } } tasks.withType<JavaCompile>().configureEach { options.release = 21 }

A reproducible local check that does not depend on shell state:

bash
mvn -q -DskipTests compile -Dmaven.compiler.release=21 ./gradlew compileJava --no-daemon -Porg.gradle.java.installations.auto-download=false

Key Comparison Table

SettingControlsAPI checkRecommended
-source 21 -target 21Syntax and bytecodeNoLegacy only
--release 21Syntax, bytecode, APIYesYes
<maven.compiler.release>Same as --releaseYesYes, plugin 3.6+
<source>/<target> elementsLegacy pairNoAvoid
Gradle options.releaseSame as --releaseYesYes
Gradle toolchainWhich JDK compilesImpliedYes
Where the wrong JDK hidesCommand to reveal itFix
Shell JAVA_HOMEecho $JAVA_HOMEExport the new JDK
Maven runtimemvn -versionSet JAVA_HOME or ~/.mavenrc
Gradle daemon./gradlew --version./gradlew --stop
IDEProject SDK / Gradle JVMSet project SDK
CI imagedocker run ... java -versionChange base image
Toolchain file~/.m2/toolchains.xmlPoint at a real JDK

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: Maven runs on an older JDK than the shell

bash
java -version # 21 mvn -version # Java version: 17

mvn resolves its JVM from JAVA_HOME first, then PATH. A stale JAVA_HOME in ~/.bashrc or a system-wide profile explains the mismatch:

bash
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 -version

On 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

xml
<!-- ~/.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>
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 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:

bash
./gradlew --stop ./gradlew --version ./gradlew compileJava

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

properties
# gradle.properties org.gradle.java.installations.auto-download=true

Auto-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

text
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
dockerfile
# wrong for builds FROM eclipse-temurin:21-jre # right FROM eclipse-temurin:21-jdk

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

kotlin
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

bash
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 with mvn -version and ./gradlew --version before 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 to PATH.
  • source and target without release. The build succeeds on a newer JDK and fails at runtime on the server's older JRE with NoSuchMethodError or UnsupportedClassVersionError.
  • Maven compiler plugin versions. maven.compiler.release is 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 21 deployment plus a 25 build produces class file has wrong version 69.0 at startup, not at compile time.
  • IDE cache after a JDK switch. Maven projects in IntelliJ keep an imported JDK per module; changing JAVA_HOME alone 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 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.