Saturday, April 30, 2022

java.lang.UnsupportedClassVersionError - Resolving UnsupportedClassVersionError in Java

In this article we’ll see what is java.lang.UnsupportedClassVersionError and how to resolve UnsupportedClassVersionError in Java.

UnsupportedClassVersionError in Java

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. In simpler terms you can say that this error is thrown when Java file is compiled by a higher version of Java compiler and you are trying to run the class file using a lower version of Java.

For example to show UnsupportedClassVersionError I have compiled a Java file using Java 12 and then tried to run it using Java 10 (lower version).

C:\Program Files\Java\jdk-10.0.1\bin>java -classpath F:\Anshu\NetJs\NetJS\src\  org.netjs.programs.FileByteRW
Error: LinkageError occurred while loading main class org.netjs.programs.FileByteRW
        java.lang.UnsupportedClassVersionError: org/netjs/programs/FileByteRW 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

Points about UnsupportedClassVersionError-

  1. It is an error which descends from java.lang.Error Since it is an error so you can’t do any exception handling to recover from it.
  2. This error is thrown at runtime.

Class file version – major and minor versions

Now the question is how does JVM determine whether the class file version is supported or not for that you have to know the class file format in Java.

Though a Java class file consists of 10 sections but there are only two sections that relate to UnsupportedClassVersionError-

  1. Magic Number which is the first 4 bytes of the class file and the value is 0xCAFEBABE. Magic number is used to uniquely identify the format.
  2. Version of class file format which are the next 4 bytes and contain the minor and major versions of the class file

If the class file version is greater that what JVM supports java.lang.UnsupportedClassVersionError is thrown.

Below table shows the major version number of the class file format being used for the Java versions. Reference - https://en.wikipedia.org/wiki/Java_class_file

Java Version Supported class version
Java SE 1761 (0x3D hex)
Java SE 1660 (0x3C hex)
Java SE 1559 (0x3B hex)
Java SE 1458 (0x3A hex)
Java SE 1357 (0x39 hex)
Java SE 1256 (0x38 hex)
Java SE 1155 (0x37 hex)
Java SE 1054 (0x36 hex)
Java SE 953 (0x35 hex)
Java SE 852 (0x34 hex)
Java SE 751 (0x33 hex)
Java SE 650 (0x32 hex)
Java SE 549 (0x31 hex)
JDK 1.448 (0x30 hex)
JDK 1.347 (0x2F hex)
JDK 1.246 (0x2E hex)
JDK 1.145 (0x2D hex)

How to resolve UnsupportedClassVersionError

Since UnsupportedClassVersionError is related to the Java version being used so there are two things that can be done based on what final Java version you want to use for your application.

  1. Compile code in an earlier version of Java if you want to supposr earlier version.
  2. Run Java code in a newer version by upgrading application Java version support.

In Eclips IDE you can reduce/increase the compiler compliance level based on your requirement. For that you need to go to Project properties – Java Compiler and then enable project specific settings to reduce or increase compiler compliance level.

Also change the configured JRE by going to Project properties – Java Build Path – libraries tab. There you can edit the chosen JRE and select an alternate JRE.

From commandline you can use --release option with javac to compile for a specific release. Supported releases are 7,8,9,10,11,12

That's all for this topic java.lang.UnsupportedClassVersionError - Resolving UnsupportedClassVersionError in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. java.lang.ClassCastException - Resolving ClassCastException in Java
  2. Java Exception Handling Tutorial
  3. throw Statement in Java Exception Handling
  4. Try-With-Resources in Java With Examples
  5. Java Exception Handling Interview Questions And Answers

You may also like-

  1. How to Sort ArrayList in Java
  2. Thread Priority in Java Multi-Threading
  3. Byte Streams in Java IO
  4. Array in Java With Examples
  5. Find Duplicate Elements in an Array Java Program
  6. Spring JdbcTemplate Select Query Example
  7. Python String isnumeric() Method
  8. Installing Hadoop on a Single Node Cluster in Pseudo-Distributed Mode