Skip to content
  1. LuzineDebug
  2. Java, Kotlin & .NET
  3. Exception in thread "main" java.lang.Unsupported…
Java, Kotlin & .NET Intermediate ≈ 10 min to fix

Fix "UnsupportedClassVersionError: compiled by a more recent version of the Java Runtime"

Error message
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
In short

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.

Be the first to confirm this fix

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

  1. Several JDKs installed: you compile with Java 21 (IDE, Gradle) but java in the terminal, JAVA_HOME or the server points to Java 17 or 11.
  2. 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:17 image with an older runtime.
  3. 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.
  4. 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

  1. 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.

    plaintext
    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
  2. See which Java really runs

    Compare the runtime version, the compiler version and JAVA_HOME. javap shows the version of a compiled class.

    bash
    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
  3. 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_HOME and PATH to it. Put the export in ~/.zshrc or ~/.bashrc to keep it.

    bash
    # 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
  4. Option 1: run with a newer JDK (Windows)

    Set JAVA_HOME for your user and put its bin folder first in PATH, then open a new terminal. where java shows which java.exe is found first.

    powershell
    [Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Eclipse Adoptium\jdk-21", "User")
    # open a new terminal, then:
    where.exe java
    java -version
  5. 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.

    kotlin
    // build.gradle.kts
    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(17)
        }
    }
    // Kotlin projects
    kotlin {
        jvmToolchain(17)
    }
  6. Option 2: compile for the older version (Maven)

    Use maven.compiler.release rather than source / target: release also checks that you do not use APIs missing from the target version. Then rebuild from scratch.

    xml
    <properties>
        <maven.compiler.release>17</maven.compiler.release>
    </properties>
    <!-- then: ./mvnw clean package -->
  7. 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). For Unsupported class file major version, update the Gradle wrapper (at least 8.5 for Java 21, 9.1 for Java 25).

    bash
    ./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-java with java-version) and in the production Docker image.
  • Add a .sdkmanrc or .java-version file 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

All Java, Kotlin & .NET errors →