Java UnsupportedClassVersionError – Causes, Examples, and Complete Fix Guide

UnsupportedClassVersionError is a common runtime error in Java that occurs when a class file has been compiled with a newer version of the Java Development Kit (JDK) but is being executed with an older version of the Java Runtime Environment (JRE) or JVM. This version mismatch prevents the JVM from understanding the class file format, causing the application to fail at startup.

If you’ve encountered an error like:

Exception in thread “main” java.lang.UnsupportedClassVersionError:

Main 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

don’t worry. In this guide, you’ll learn what UnsupportedClassVersionError is, why it occurs, common causes, and step-by-step solutions to fix and prevent it.

What is UnsupportedClassVersionError?

UnsupportedClassVersionError is a runtime error that occurs when the JVM tries to load a compiled .class file created with a newer Java version than the runtime supports.

Every Java release generates class files with a specific class file version. Older JVMs cannot execute class files created for newer Java releases.

For example:

  • Compile with JDK 21
  • Run with JRE 17

The application starts, but the JVM throws:

java.lang.UnsupportedClassVersionError

because Java 17 cannot understand Java 21 class files.

Why Does UnsupportedClassVersionError Occur?

The primary cause is a Java version mismatch between compilation and execution.

Common scenarios include:

  • Compiling with a newer JDK
  • Running with an older JRE
  • Incorrect JAVA_HOME
  • Incorrect PATH
  • Mixed Java versions in an IDE
  • Build server using a different JDK
  • Maven or Gradle configured with a higher Java version
  • Docker image using an older Java runtime

Understanding Java Class File Versions

Each Java release corresponds to a class file version.

Java Version Class File Version
Java 8 52.0
Java 9 53.0
Java 10 54.0
Java 11 55.0
Java 12 56.0
Circular object creation 4 or 8 bytes
Java 13 57.0
Java 14 58.0
Java 15 59.0</td
Java 16 60.0
Java 17 61.0
Java 18 62.0
Java 19 63.0

If your runtime only supports version 61.0 but the class file is version 65.0, UnsupportedClassVersionError occurs.

Solution 1: Upgrade the Java Runtime

The simplest solution is to install a Java runtime that matches or exceeds the version used for compilation.

Check the runtime version:

java -version

Example output:

openjdk version “17”

If your application was compiled with Java 21, install Java 21 or later.

Solution 2: Verify the Compiler Version

Check which JDK compiled your application.

javac -version

Example:

javac 21

If javac reports Java 21 while java -version reports Java 17, you have a version mismatch.

Solution 3: Compile for an Older Java Version

If your application must run on an older JVM, compile it for that version.

Example:

javac –release 17 Main.java

Or:

javac -source 17 -target 17 Main.java

Using –release is generally recommended because it ensures both language features and APIs match the target Java version.

Solution 4: Check JAVA_HOME

Verify the JAVA_HOME environment variable.

Windows:

echo %JAVA_HOME%

Linux/macOS:

echo $JAVA_HOME

Ensure it points to the correct JDK installation.

Example:

C:\Program Files\Java\jdk-21

An incorrect JAVA_HOME can cause your build tool or IDE to use an unintended JDK.

Solution 5: Verify the PATH Variable

The PATH environment variable determines which Java executable runs.

Check:

java -version

If it displays an unexpected version, update the PATH so that the desired JDK’s bin directory appears first.

Solution 6: Configure Maven Correctly

For Maven projects, specify the Java version in pom.xml.

Example:


<properties>
    <maven.compiler.release>17</maven.compiler.release>
</properties>

Or:
<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
</properties>

After making changes, rebuild the project:

mvn clean package

Solution 7: Configure Gradle Correctly

In Gradle, configure the Java toolchain.

Example:


java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

Or set compatibility:

sourceCompatibility = JavaVersion.VERSION_17

targetCompatibility = JavaVersion.VERSION_17

Then rebuild the project:

gradle clean build

Solution 8: Check IDE Configuration

Many developers have multiple JDKs installed.

Verify your IDE uses the correct JDK.

For IntelliJ IDEA:

  • Project SDK
  • Project Language Level
  • Gradle JVM
  • Maven JVM
  • Run Configuration

For Eclipse:

  • Installed JREs
  • Compiler Compliance Level
  • Build Path

For VS Code:

  • Java Runtime Configuration
  • Project SDK

Keep the IDE, compiler, and runtime versions consistent.

Solution 9: Verify Docker Images

Containerized applications often fail because the Docker image contains an older Java runtime.

Incorrect:


FROM openjdk:17

If the application was compiled with Java 21, use:

FROM eclipse-temurin:21-jdk

Always match the runtime image with the compilation target.

Solution 10: Check CI/CD Build Servers

Build servers such as Jenkins, GitHub Actions, or GitLab CI may use a different JDK than your local machine.

Verify:

  • Build JDK version
  • Deployment runtime version
  • Build scripts
  • Environment variables

Keeping build and production environments aligned helps avoid version mismatch errors.

Solution 11: Clean and Rebuild the Project

Old compiled class files may remain after changing the Java version.

For Maven:

mvn clean package

For Gradle:

gradle clean build

Delete old build directories such as:

target/

or

build/

before rebuilding.

Solution 12: Verify Third-Party Libraries

Sometimes your own code targets Java 17, but a dependency was compiled with Java 21.

Check dependency versions using:

Maven:

mvn dependency:tree

Gradle:

gradle dependencies

If a dependency requires a newer Java version, either:

  • Upgrade your runtime.
  • Replace the dependency with a compatible version.
  • Use an older library release.

Real-World Example

Suppose you compile:


javac Main.java

using Java 21.

Then run:

java Main

on Java 17.

Output:

Exception in thread “main”
java.lang.UnsupportedClassVersionError:
Main has been compiled by a more recent version of the Java Runtime

Solution:

  • Install Java 21 or
  • Recompile using:

javac –release 17 Main.java

Common Scenarios That Cause UnsupportedClassVersionError

Scenario Solution
Compiled with newer JDK Upgrade the runtime or recompile for the target version
Older JRE installed Install a compatible Java runtime
Incorrect JAVA_HOME Update JAVA_HOME to the desired JDK
Incorrect PATH Ensure the correct Java executable is used
Maven version mismatch Configure maven.compiler.release or source/target
Gradle toolchain mismatch onfigure the Java toolchain correctly
IDE using the wrong JDK Update project SDK and compiler settings
Docker runtime too old Use a compatible Java base image
CI/CD build mismatch Align build and deployment JDK versions</td
Dependency compiled for a newer Java version Dependency compiled for a newer Java version

Best Practices to Prevent UnsupportedClassVersionError

Follow these recommendations:

  • Use the same Java version for development, testing, and production.
  • Prefer the –release option when compiling applications for older JVMs.
  • Configure Maven and Gradle explicitly with the target Java version.
  • Keep JAVA_HOME and PATH synchronized.
  • Document the required Java version in your project.
  • Use consistent Docker base images across environments.
  • Verify third-party library compatibility before upgrading dependencies.
  • Clean and rebuild the project after changing Java versions.
  • Test deployments in an environment that matches production.
  • Regularly update your JDK and build tools to supported versions.

Frequently Asked Questions (FAQs)

Is UnsupportedClassVersionError a compile-time error?

No. It is a runtime error that occurs when the JVM attempts to load a class file compiled for a newer Java version than it supports.


What is the main cause of UnsupportedClassVersionError?

The primary cause is a mismatch between the Java version used to compile the application and the Java runtime used to execute it.


How do I fix UnsupportedClassVersionError?

You can fix it by:

  • Installing a newer Java runtime.
  • Compiling the application for the required Java version using –release.
  • Configuring Maven or Gradle correctly.
  • Updating JAVA_HOME and PATH.
  • Ensuring all environments use compatible Java versions.

Should I use –release or -source and -target?

For modern Java development, –release is generally the preferred option because it configures the language level, generated bytecode, and available APIs consistently for the target Java version.


Conclusion

UnsupportedClassVersionError is a common Java runtime error caused by a mismatch between the Java version used for compilation and the version used for execution. It typically occurs when newer class files are run on an older JVM. By keeping your JDK, JRE, build tools, IDE settings, and deployment environments aligned, and by compiling with the appropriate target version using –release, you can eliminate this error and ensure your Java applications run reliably across different environments.

Related Java Tutorials