Fix for java.lang.UnsupportedClassVersionError in Java

The UnsupportedClassVersionError is a runtime error that occurs when Java code is compiled and run with incompatible and unsupported Java versions. UnsupportedClassVersionError class extends ClassFormatError, a subclass of LinkageError.

This runtime error is thrown by the JVM when attempting to run a Java class file with major and minor versions that are not supported. The error stack trace contains a message like “Unsupported major.minor version x.x”, with x.x corresponding to the version of the installed Java.

The simple fix is to ensure that you install both JDK and JRE with the same version. For instance, if you installed JDK 11, use JRE 11, not JRE 8 or any other versions.

This error arises when running Java programs with an incompatible version of compiled and running programs. It usually occurs when a Java file is compiled with JDK 11 and then run using JDK 8.

To understand what caused this error, consider the following major.minor version table for different Java versions:

JDK VersionMajor Number
1.145
1.246
1.346
1.447
1.548
1.649
1.750
1.851
1.952
1.1053
1.1154
1.1255
1.1356
1.1457
1.1558
1.1659
1.1760
1.1861
1.1962
1.2063
1.2164

Solution for java.lang.UnsupportedClassVersionError Error

First, check the Java and javac versions using the command line.

For example:

C:\myapp>javac -version
javac 1.8.0_102

And for the runtime version.

C:\myapp>java -version
java version "10.0.2" 2018-07-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)

In this case, both have different versions.

To resolve, upgrade either JDK or JRE to the same version. Once the same version is set, recompile and run the code. This error may occur due to:

  • Compilation of code running a different, higher Java version.

  • The runtime of your code running with a lower Java version.

    This error comes in eclipse as well as command line

Fix for UnsupportedClassVersionError Error in Eclipse

When you run java code, First change your project settings point to JDK, Next set the java compiler to a specific java version. Here are steps

  • Right-click on your project —> Properties —> Java Build PathLibraries
  • Select Java libraries as shown below, and choose Edit and select target JRE
Configure JDK in eclipse

Next, set the java compiler version and target level.

Please check below screenshot to do eclipse configuration

Configure Java compiler in eclipse

Fix for UnsupportedClassVersionError Error via Command Line

This error can also be fixed using the source and target command-line options. Provide source and target options with your version:

javac -source 1.8 -target 1.8  javafile.java

This ensures compatibility with backward versions.

Fix for UnsupportedClassVersionError Error for Maven Project

To fix this error in a Maven project, add source and target options for the Maven compiler plugin. This compiles and runs your project with the same version.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>1.10</source>
        <target>1.10</target>
    </configuration>
</plugin>

Conclusion

This post addressed the cause of UnsupportedClassVersionError and provided fixes for command line, Eclipse, and Maven projects, dealing with compile and runtime version mismatches.