Fix "UnsupportedClassVersionError: compiled by a more recent version of the Java Runtime"
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/example/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
- java.lang.UnsupportedClassVersionError: org/springframework/boot/SpringApplication has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
- Unsupported major.minor version 52.0
- class file has wrong version 61.0, should be 55.0
- Unsupported class file major version 65
- error: invalid target release: 21
The code was compiled for a newer Java version than the one running it (here class 65 = Java 21, JVM 61 = Java 17). Run it with a JDK at least as recent (fix JAVA_HOME and the IDE JDK), or recompile for the older version with --release, the Gradle toolchain or maven.compiler.release.
What this error means
Each .class file carries a version number that depends on the compilation target: 52 for Java 8, 55 for Java 11, 61 for Java 17, 65 for Java 21, 69 for Java 25. A JVM can read classes of its own version and older, never newer. When it loads a newer class, it throws UnsupportedClassVersionError with both numbers: the class version and the maximum it accepts. The offending class can be yours or a dependency’s (Spring Boot 3 and 4 are compiled for Java 17). The Unsupported class file major version variants come from tools (Gradle, ASM) too old for the JDK running them.
Common causes
- Several JDKs installed: you compile with Java 21 (IDE, Gradle) but
javain the terminal,JAVA_HOMEor the server points to Java 17 or 11. - Server or Docker image too old: the JAR is built in CI with a recent JDK, then deployed to a machine or an
eclipse-temurin:17image with an older runtime. - Dependency that requires a newer Java: Spring Boot 3 or 4, Hibernate 6 or a recent library need Java 17 while your project runs on Java 8 or 11.
- Build tool too old for the JDK (
Unsupported class file major version 65): an old Gradle version launched with Java 21 or 25.
Step-by-step fix
-
Translate the numbers into Java versions
Java version = class file number minus 44. The first number is the compilation target, the second is the JVM running the code.
class file 52 -> Java 8 class file 55 -> Java 11 class file 61 -> Java 17 class file 65 -> Java 21 class file 69 -> Java 25 -
See which Java really runs
Compare the runtime version, the compiler version and
JAVA_HOME.javapshows the version of a compiled class.java -version javac -version echo $JAVA_HOME javap -v build/classes/java/main/com/example/App.class | grep major # macOS: list installed JDKs /usr/libexec/java_home -V -
Option 1: run with a newer JDK (macOS / Linux)
Install a JDK at least equal to the compilation version (Temurin, Zulu, Oracle…), then point
JAVA_HOMEandPATHto it. Put theexportin~/.zshrcor~/.bashrcto keep it.# macOS export JAVA_HOME=$(/usr/libexec/java_home -v 21) export PATH="$JAVA_HOME/bin:$PATH" # Debian / Ubuntu sudo apt install openjdk-21-jdk sudo update-alternatives --config java -
Option 1: run with a newer JDK (Windows)
Set
JAVA_HOMEfor your user and put itsbinfolder first inPATH, then open a new terminal.where javashows whichjava.exeis found first.[Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Eclipse Adoptium\jdk-21", "User") # open a new terminal, then: where.exe java java -version -
Option 2: compile for the older version (Gradle)
If the target must stay on Java 17, set the toolchain: Gradle uses or downloads the right JDK whatever your machine has. With Kotlin,
jvmToolchain(17)does the same.// build.gradle.kts java { toolchain { languageVersion = JavaLanguageVersion.of(17) } } // Kotlin projects kotlin { jvmToolchain(17) } -
Option 2: compile for the older version (Maven)
Use
maven.compiler.releaserather thansource/target:releasealso checks that you do not use APIs missing from the target version. Then rebuild from scratch.<properties> <maven.compiler.release>17</maven.compiler.release> </properties> <!-- then: ./mvnw clean package --> -
Align the IDE, Gradle and deployment
IntelliJ: File > Project Structure > SDK, and Settings > Build Tools > Gradle > Gradle JVM. Android Studio: Gradle JDK. Docker: use a runtime image of the same version (
eclipse-temurin:21-jre). ForUnsupported class file major version, update the Gradle wrapper (at least 8.5 for Java 21, 9.1 for Java 25)../gradlew wrapper --gradle-version latest ./gradlew --version
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Declare the Java version in the build (Gradle toolchain,
maven.compiler.release) instead of relying on the JDK installed on each machine. - Use the same JDK version locally, in CI (
actions/setup-javawithjava-version) and in the production Docker image. - Add a
.sdkmanrcor.java-versionfile to the repository so everyone switches to the right JDK automatically.
Frequently asked questions
Which Java version is class file version 65.0?
Java 21. The rule is simple: Java version = class file number − 44 (61 = Java 17, 55 = Java 11, 52 = Java 8, 69 = Java 25).
I installed Java 21 but java -version still shows 17, why?
Another java is found first in PATH, or JAVA_HOME still points to the old JDK. Fix the PATH order, then open a new terminal.
Can I run Spring Boot 3 on Java 11?
No: Spring Boot 3 and 4 are compiled for Java 17 minimum. Move the runtime to Java 17 or later, or stay on Spring Boot 2.7 (no longer maintained in open source).
Useful tools for this error
More Java, Kotlin & .NET errors
- Fix "java.lang.NullPointerException: Cannot invoke … because … is null" in Java
- Fix "Could not resolve all files for configuration" and "Could not find" in Gradle
- Fix "Web server failed to start. Port 8080 was already in use" in Spring Boot
- Fix "Failed to configure a DataSource: 'url' attribute is not specified" in Spring Boot
- Fix "java.lang.OutOfMemoryError: Java heap space" (and GC overhead limit exceeded)
- Fix "Object reference not set to an instance of an object" (NullReferenceException) in C#
- Fix "lateinit property binding has not been initialized" in Kotlin (Android)