Fix for java.lang.UnsupportedClassVersionError in Java

UnsupportedClassVersionError is a runtime error thrown when Java code is compiled and run with incompatible and unsupported java versions.

UnsupportedClassVersionError extends ClassFormatError which is a sub class of LinkageError.

This is a runtime error thrown by JVM as it tried to run a java class file that has major and minor versions that are not supported.

error stack trace contains a message like an Unsupported major.minor version x.x.

x.x is a number corresponding to the version of your installed java version.

The simple fix is first to make sure that you install JDK and JRE with the same version.

if you installed JDK 11 — JRE 11, not JRE 8 or JRE 9 or any versions.

JDK 9 — JRE 9, not JRE 10 or JRE 9 and not other versions

This error comes when running java programs with an incompatible version of compile and running programs. Let’s try to understand what caused this error.

java file was compiled with JDK 11 and running the java code using JDK 8 version.

Please check the below table for major.minor version 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

Solution for java.lang.UnsupportedClassVersionError error

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

Here is a compiler version

C:\myapp>javac -version
javac 1.8.0_102

Runtime version using java command

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)

Both have different versions.

This error comes when running java code with different versions.
First, upgrade either JDK or JRE to the same version. Once the same version is set, compile again and run the code.

This error will come in the following cases

  • Compilation of code runs different higher java version
  • The runtime of your code runs 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 of the version

Please check below screenshot to do eclipse configuration

Configure Java compiler in eclipse

Fix for UnsupportedClassVersionError error via command line

We can also fix this error using the source and target command-line options. Provide source and target options with your version as specified

javac -source 1.8 -target 1.8  javafile.java

This code is compatible with backward versions

Fix for UnsupportedClassVersionError error for maven project

Fix for UnsupportedClassVersionError in maven project is add source and target options for 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 talked about UnsupportedClassVersionError cause and fixing command line with compile and runtime version mismatch, fix in eclipse and maven projects.