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.
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.
1 2 3 4 5 6 7 8 9 10 11 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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
- 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.
123456pankaj:temp pankaj$ javac HelloWorld.javapankaj:temp pankaj$ java HelloWorldHello Worldpankaj:temp pankaj$ - 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