Filed Under: Java

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.



  • 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 VersionClass Major.Minor version
5 (1.5)49.0

Java UnsupportedClassVersionError Class Diagram

Below image shows the UnsupportedClassVersionError class diagram.
java 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.

pankaj:temp pankaj$ java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
pankaj:temp pankaj$ /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/bin/java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
pankaj:temp pankaj$

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.

pankaj:temp pankaj$ /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/bin/javac 
pankaj:temp pankaj$ java HelloWorld
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(
	at Method)
	at java.lang.ClassLoader.loadClass(
	at sun.misc.Launcher$AppClassLoader.loadClass(
	at java.lang.ClassLoader.loadClass(
	at sun.launcher.LauncherHelper.checkAndLoadMain(
pankaj:temp pankaj$

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.
    pankaj:temp pankaj$ javac 
    pankaj:temp pankaj$ java HelloWorld
    Hello World
    pankaj:temp pankaj$
  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


  1. Sankararaman Vedagiri says:

    I have a question here. Does this exception occur only when a feature of the earlier version is no longer supported by the latest version?

    1. Pankaj says:

      Java is backward compatible, so any program compiled on java 7 will run fine on java 8, 9 or any latest versions. Problem is when you want to run a program on older version, java throws this error because it doesn’t know if the program will work fine or not. For example, you can write a java program with lambda expressions and compile using Java 8. Now if you will try to run it using Java 7, you will get UnsupportedClassVersionError. It’s a fail-fast approach because we don’t know what will be the outcome later on.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors