Monday, August 13, 2018

Fix for java.lang.UnsupportedClassVersionError: Unsupported major.minor version issue in java

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

UnsupportedClassVersionError java example
UnsupportedClassVersionError extends ClassFormatError which is a sub class of  LinkageError.
This is not compiled time error but runtime error thrown when JVM tries to run a java class file with major and minor version are not supported.
error stack trace contains a message like an Unsupported major.minor version x.x.
x.x is a number corresponding 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.
JDK 10 -- JRE 10, not JRE 8 or JRE 9 and not other 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 program. This is an error case.
java file was compiled with JDK 10
Run the java code using JDK 1.8

Please check below table for major.minor version for different java versions.
JDK Version Major Number
1.1 45
1.2 46
1.3 46
1.4 47
1.5 48
1.6 49
1.7 50
1.8 51
1.9 52
1.10 53

UnsupportedClassVersionError Solution 

 In the command line, First, check the version of javac and java command as below

C:\myapp>javac -version
javac 1.8.0_102

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.
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
1. Compilation of code runs different higher java version
3. Execution of your code runs with 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 java compiler to specific java version. Here are steps


  • Right click on your project --> Properties --> Java Build Path -- Libraries
  • Select Java libraries as shown below, and choose Edit and select target JRE

Configure JDK in eclipse

  • Next set java compiler version target level the version

      Please check below to do eclipse configuration


Configure Java compiler in eclipse


Fix for UnsupportedClassVersionError error via command line

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>

Related article


EmoticonEmoticon