Encountering the java.lang.UnsupportedClassVersionError
is a common issue for Java developers. This error occurs when your Java Runtime Environment (JRE) is older than the version used to compile the class file. This guide explains why this error happens and provides step-by-step solutions to resolve it quickly.
Understanding the Error
The error message typically looks like this:
java.lang.UnsupportedClassVersionError: Unsupported major.minor version 65.0
The class file was compiled with a newer Java Development Kit (JDK) than the JRE version running it. For example, compiling with JDK 21 (class version 65) but running on JRE 17 (supports up to 61) will trigger this error.
Java Class File Version Compatibility
Each Java version has a unique major.minor version number:
Java Version | Major Version |
---|---|
Java 23 | 67 |
Java 22 | 66 |
Java 21 | 65 |
Java 17 | 61 |
Java 11 | 55 |
Java 8 | 52 |
Java 7 | 51 |
Source: Oracle’s Java documentation.
How to Fix the Error
Update Your JRE/JDK
Your JRE is older than the JDK used to compile the class. Install the latest JRE/JDK matching the compilation version.
Check your current Java versions:
java -version # Check JRE version
javac -version # Check JDK version
Ensure both versions match.
Download and install the latest JDK from Oracle JDK or OpenJDK, selecting the version that matches your compiled class (e.g., JDK 21 for major version 65).
Set up environment variables:
Windows:
setx JAVA_HOME "C:\Program Files\Java\jdk-21"
setx PATH "%PATH%;%JAVA_HOME%\bin"
macOS/Linux:
export JAVA_HOME=/usr/lib/jvm/jdk-21
export PATH=$JAVA_HOME/bin:$PATH
Recompile with an Older Target Version
If updating the JRE is not possible, recompile the code to match the older JRE.
Using javac
:
javac -target 1.8 -source 1.8 YourFile.java
Replace 1.8
with the required JRE version (e.g., 1.7
for Java 7).
Using build tools:
Maven:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Gradle:
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
Fix IDE-Specific Configurations
In Eclipse, go to Window > Preferences > Java > Compiler and set Compiler compliance level to match your JRE (e.g., 17 for Java 17).
In IntelliJ IDEA, navigate to File > Project Structure > Project Settings > Project, then set Project SDK and Project language level to the correct version.
Common Mistakes to Avoid
JAVA_HOME
should point to the correct JDK directory.PATH
should include the JDK’sbin
directory.- Ensure both
java
(JRE) andjavac
(JDK) versions align.
Summary
Issue | Solution |
---|---|
JRE older than compiled class | Update JRE/JDK to match the class version |
Can’t update JRE | Recompile code with -target and -source flags |
Incorrect environment variables | Set JAVA_HOME and PATH to the correct JDK |
By aligning your JDK (compile-time) and JRE (runtime) versions, you’ll eliminate the UnsupportedClassVersionError
and ensure smooth execution of your Java applications.
Keywords: Fix UnsupportedClassVersionError, Java version mismatch, major.minor version error, update Java runtime, JDK JRE compatibility.