How to Resolve the ‘Class File Has Wrong Version’ Error in Java
The “class file has wrong version” error in Java is a common issue that we encounter when running or compiling Java programs. This error occurs when the Java compiler or runtime attempts to use a .class
file that was compiled with a newer Java version than the one being used to execute it. Understanding the causes of this error and how to fix it is essential for smooth Java development.
1. Cause of the Error
The main reason for the "class file has wrong version"
error is a mismatch between the Java version used to compile the source code and the version used to run it. Each version of Java assigns a class file version number when compiling code. For example:
Java Version | Class File Version |
---|---|
Java 8 | 52.0 |
Java 11 | 55.0 |
Java 17 | 61.0 |
Java 21 | 65.0 |
If a .class
file is compiled using Java 17 (class file version 61.0) but executed using Java 11 (which supports only up to 55.0), the error occurs.
2. Example of the Error
Scenario:
- Compile a Java program using JDK 17.
- Try to run the compiled
.class
file using JDK 11.
Sample Code:
1 2 3 4 5 | public class VersionTest { public static void main(String[] args) { System.out.println( "Running Java Version Test!" ); } } |
Steps to Reproduce the Error:
- Compile with Java 17
1 | javac VersionTest.java |
- Run with Java 11
1 | java VersionTest |
Output:
1 2 | Error: LinkageError occurred while loading main class VersionTest java.lang.UnsupportedClassVersionError: VersionTest 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 |
This error means the class file was compiled with Java 17 (version 61.0) but is being run with Java 11 (which supports up to version 55.0).
3. Solution to Fix the Error
The simplest solution is to use a Java runtime that matches or exceeds the version used to compile the code. You can check your installed Java version with:
3.1 Setting the Java Version in Build Tools
To ensure compatibility and prevent version-related errors, we need to configure the Java version in your build tool. If you are using Maven, specify the required Java version in your pom.xml
file to ensure the project compiles with the intended JDK:
1 2 3 4 5 6 | < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < maven.compiler.source >17</ maven.compiler.source > < maven.compiler.target >17</ maven.compiler.target > < exec.mainClass >com.jcg.example.VersionTest</ exec.mainClass > </ properties > |
This ensures the code is compiled to be compatible with Java 17.
4. Conclusion
In this article, we explored the causes and solutions for the "class file has wrong version"
error in Java. This issue arises due to a mismatch between the Java version used for compilation and the version used for execution. We demonstrated how this error can occur in a Java application and provided a way to resolve it, by correctly configuring Maven build tools.
By ensuring that your Java environment is properly set up and that your build tools specify the correct Java version, we can avoid compatibility issues and ensure smooth execution of our applications.
5. Download the Source Code.
This article covered the “Class File Has Wrong Version” error.
You can download the full source code of this example here: Error – class file has wrong version