UnsupportedClassVersionError in Java and Resolution

In this post we’ll discuss about java.lang.UnsupportedClassVersionError and how to fix it.

When is UnsupportedClassVersionError in Java thrown

UnsupportedClassVersionError is thrown when the Java Virtual Machine attempts to read a class file whose major and minor version numbers are not supported by the current JVM version. To understand it better you need some background on class file format and what are major and minor versions.

Java Virtual Machine class file format contains many sections, for UnsupportedClassVersionError the section of interest is the second section which tells the version of the class file format. This section is of 4 bytes, where 2 bytes are allotted to minor_version and 2 bytes to major_version. Together, a major and a minor version number determine the version of the class file format.

If the class file version is greater than what JVM supports java.lang.UnsupportedClassVersionError is thrown. The release level of the Java SE platform to which a Java Virtual Machine implementation conforms is responsible for determining the range of the major and minor versions supported by the JVM.

The major version number of a class file is derived from the Java version being used.

Java Version Supported class version
Java SE 14 58 (0x3A hex)
Java SE 13 57 (0x39 hex)
Java SE 12 56 (0x38 hex)
Java SE 11 55 (0x37 hex)
Java SE 10 54 (0x36 hex)
Java SE 9 53 (0x35 hex)
Java SE 8 52 (0x34 hex)
Java SE 7 51 (0x33 hex)
Java SE 6 50 (0x32 hex)
Java SE 5 49 (0x31 hex)
JDK 1.4 48 (0x30 hex)
JDK 1.3 47 (0x2F hex)
JDK 1.2 46 (0x2E hex)
JDK 1.1 45 (0x2D hex)

UnsupportedClassVersionError in Java example

Here is an example where UnsupportedClassVersionError is thrown. Java file is compiled using Java 12 compiler and then the class is executed using Java 10.

C:\Program Files\Java\jdk-10.0.1\bin>java -classpath F:\knpcodews\src\  com.knpcode.programs.Test
Error: LinkageError occurred while loading main class com.knpcode.programs.Test
        java.lang.UnsupportedClassVersionError: com/knpcode/programs/Test has been compiled by a more recent version of the Java Runtime 
  (class file version 56.0), this version of the Java Runtime only recognizes class file versions up to 54.0

Java UnsupportedClassVersionError hierarchy

UnsupportedClassVersionError is a descendant of java.lang.Error. Since it is of type Error so you can’t do any exception handling to recover from it.
UnsupportedClassVersionError in Java

How to fix UnsupportedClassVersionError

UnsupportedClassVersionError is thrown when the JVM used to compile Java file and the JVM used to execute the Java class are not compatible so there are two options-

1. Use the higher Java version to execute the Java class.
2. If you can’t use higher Java version then try to compile Java code files using the Java version that is used to run the application.

If you are using Eclipse IDE then you can select the required version of JRE by going to Window – Preferences – Java – Installed JREs. Click on Add and select the JDK from the installed folder.
Eclipse installed JREs
You can also increase or decrease the compiler compliance level based on your requirement. Go to Project (from menu or right click current project) – properties – Java Compiler and then enable project specific settings to set compiler compliance level for the project.

Related Posts

That’s all for the topic UnsupportedClassVersionError in Java and Resolution. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.