java.lang.unsupportedclassversionerror – How to handle Unsupported Class Version Error
In this tutorial we will discuss about Java’s UnsupportedClassVersionError
and how to deal with it. The UnsupportedClassVersionError
is a sub-class of the LinkageError
class and specifically, of the ClassFormatError
class. This error is thrown by the Java Virtual Machine (JVM) when it tries to read a class file whose major and minor version numbers are not supported. This error exists since the Java 1.2 release and is thrown only at runtime and especially during the linking phase.
The UnsupportedClassVersionError
indicates that the current JVM does not support your application’s class file. To begin with, we must understand how the JVM decides that it cannot support a specific class file. To achieve that, we must first describe the format of a class file.
Each class file contains Java bytecode that is executed by the JVM. Class files are identified by the Magic Number 0xCAFEBABE
, which occupies the first 4 bytes of a class file. The next 4 bytes are used to indicate the minor and major versions of the class file. If the JVM identifies that the versions in a class file cannot be supported, it throws an UnsupportedClassVersionError
. This error occurs when you compile a program using a higher version of Java and execute it using a JVM of a lower version. However, the vice-versa is not true.
The major version number of a class file is interpreted as follows:
Value | Java version |
51 (33 hex) | JDK 1.7 |
Deal with the UnsupportedClassVersionError
First of all, we must verify the installed version of Java in our system. Using the terminal (Linux or Mac) or the command prompt (Windows), we execute the following command:
java –version
A sample output of the command is shown below:
java version "1.7.0_45" Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
In this example, we verify that the installed Java version is the 1.7.0_45
.
The following example can be used to find the major version number of a class file:
FindMajorVersionNumber.java:
import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class FindMajorVersionNumber { public static void main(String[] args) throws IOException { // Verify that at least one argument has been passed to the application. if(args.length < 1) throw new RuntimeException("Usage: FindMajorVersionNumber <path_to_class_file>"); // Create a buffer to store the first 8 bytes of a file. byte[] buffer = new byte[8]; // Create an InputStream from the file provided as an argument. InputStream is = new DataInputStream(new FileInputStream(new File(args[0]))); // Read the first 8 bytes of the file. is.read(buffer); // Print the major version number. System.out.println(buffer[7] + " (" + String.format("0x%x", buffer[7]) + ")"); // Close the stream. is.close(); } }
In this example, we read the first 8 bytes of a class file provided as an argument to our application and then, we print the major version number in both decimal and hexadecimal format. You can take advantage of this program, in order to install the appropriate version of Java in your system.
Another very good way to deal with this error is to upgrade the installed Java of your system to its latest version. In this way, you can be absolutely certain that all Java applications can be successfully executed in your system.
Final comments on the Error
It is very important to discuss and notice that:
- The error cannot be detected during the compilation of your application. It is thrown only on the runtime, during the linking phase.
- It’s a good practice to use the same version of Java for both compilation and execution of your applications, in order to avoid the aforementioned error.
- The
UnsupportedClassVersionError
is not related to your application’s classpath. - In case your application depends on external .jar files, make sure that you have the appropriate JVM in order to execute them.
This was a tutorial on Java’s UnsupportedClassVersionError
.