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.
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.
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.
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
- ClassCastException in Java and Resolution
- InputMismatchException in Java and Resolution
- StackOverflowError Vs OutOfMemoryError in Java
- How to Create Custom Exception Class in Java
- Java Multi-Catch Exception With Examples
- Java try-with-resources With Examples
- Java try-catch Block With Examples
- Java Exception Handling Interview Questions And Answers
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