java.lang.UnsupportedClassVersionError

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.

java.lang.UnsupportedClassVersionError

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.
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 HelloWorld.java 
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(ClassLoader.java:763)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
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 HelloWorld.java 
    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

Comments

  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.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages