How to Fix the java.lang.UnsupportedClassVersionError
This tutorial demonstrates the Exception in thread main java.lang.UnsupportedClassVersionError
error in Java.
Cause of the java.lang.UnsupportedClassVersionError
The UnsupportedClassVersionError
is a sub-class of the ClassFormatError
exception, which is thrown when JVM tries to read a class and finds that the class file is malformed or the file cannot be interpreted as a class. Here is the hierarchy of the UnsupportedClassVersionError
exception:
Java.Lang.Object
Java.Lang.Throwable
Java.Lang.Error
Java.Lang.LinkageError
Java.Lang.ClassFormatError
Java.Lang.UnsupportedClassVersionError
The UnsupportedClassVersionError
exception is specific to detecting a class file run by a lower version of Java before and now run by a newer version.
For example, suppose a Java file was run by JDK 12 and now by a newer JRE 8. In that case, it will throw the UnsupportedClassVersionError
exception, or if we compile the class using Java version 1.8 and compile it using Java 1.7, it will throw the same error.
Let’s try an example in a class compiled by the newer version of Java and then run by an older version. See example:
public class Unsupported_Class_Version_Error{
public static void main(String args[]) {
System.out.println("Hello this is Delftstack.com");
}
}
The code above will throw the UnsupportedClassVersionError
as shown below:
Exception in thread "main" java.lang.UnsupportedClassVersionError: Unsupported_Class_Version_Error : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
The output shows the error because the code is compiled at Java 1.8 and executed at Java 1.7. That is why Exception in thread "main" java.lang.UnsupportedClassVersionError: Unsupported_Class_Version_Error : Unsupported major.minor version 52.0
will be thrown.
Fix the java.lang.UnsupportedClassVersionError
There are two conditions for this error, so the solution lies in these two conditions:
- Run the code with the latest JDK and JRE.
- Compile the code with the older version of JDK to match the runtime JDK.
- One simple solution is to use the Java Cross Compilation. If the Production Environment JDK is lower than the Build Environment, we can generate a class file with a lower version using cross-compilation.
The following command will be used to create a class file for the code above:
javac -target 1.7 Unsupported_Class_Version_Error.java
As mentioned above, the solution is to build the application with the same versions at compile and runtime, or at least the compile-time version is lower than the runtime version.
The error above shows the Major Minor versions problem. Major Minor versions are the versions of JRE; for example, for JRE 8, the Major version is 52.0.
We must ensure no Major Minor version problems occur while compiling and executing the Java Class file. The list for the versions of JRE which are compatible with the class are:
Java SE 17 = 61,
Java SE 16 = 60,
Java SE 15 = 59,
Java SE 14 = 58,
Java SE 13 = 57,
Java SE 12 = 56,
Java SE 11 = 55,
Java SE 10 = 54,
Java SE 9 = 53,
Java SE 8 = 52,
Java SE 7 = 51,
Java SE 6.0 = 50,
Java SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn FacebookRelated Article - Java Error
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix the Missing Server JVM Error in Java
- How to Fix the 'No Java Virtual Machine Was Found' Error in Eclipse
- How to Fix Javax.Net.SSL.SSLHandShakeException: Remote Host Closed Connection During Handshake
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix Java.Lang.VerifyError: Bad Type on Operand Stack