java.lang.UnsupportedClassVersionError With Examples

java.lang.UnsupportedClassVersionError is one of the runtime error and thrown when a class is compiled by a more recent version that the current java version.

java.lang.UnsupportedClassVersionError

  • UnsupportedClassVersionError is an error, so we can’t do anything about it from application point of view to recover from it.
  • java.lang.UnsupportedClassVersionError is a runtime error, it doesn’t come at compile time.
  • Every java class contains a major.minor version number, this number denotes the java compiler version. Whenever we execute a java class and the class file version number is greater than current java version number, then java.lang.UnsupportedClassVersionError is thrown.

Java Version and Class Major.Minor Version

Below table lists the java version and corresponding class file major.minor version.

Java Version Class Major.Minor version
1.1 45.3
1.2 46.0
1.3 47.0
1.4 48.0
5 (1.5) 49.0
6 50.0
7 51.0
8 52.0
9 53.0

Java UnsupportedClassVersionError Class Diagram

Below image shows the UnsupportedClassVersionError class diagram.

UnsupportedClassVersionError-class-diagram

java.lang.UnsupportedClassVersionError Example

We need two java versions to produce UnsupportedClassVersionError, I have both Java 8 and java 9 installed in my system with java 8 being the default one.

I created a simple hello world class in the some directory for our example and then compiled it with java 9 and then tried to run it with java 8.

So we were able to reproduce UnsupportedClassVersionError, the stack trace clearly shows the “class file version 53.0” that corresponds to Java 9 and current java supports version up to 52.0 that corresponds to java 8.

How to Fix java.lang.UnsupportedClassVersionError

  1. If you have access to java source files, then you can recompile the class again and run it with the same version. So I can either compile HelloWorld class again or run it with java 9, both will work fine.
  2. What if you don’t have access to source files such as third party jar files?In that case we will have to upgrade our java version to the one able to support it. Since stack trace clearly specifies the class file version number, so we know which java version to install to execute the program.

That’s all for a quick roundup on java.lang.UnsupportedClassVersionError, it’s a very simple error to fix.

Reference: API Doc, Exception Handling in java

By admin

Leave a Reply

%d bloggers like this: