Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Monday, August 2, 2021

2 ways to solve Unsupported major.minor version 51.0 error in Java

Unsupported major.minor version 51.0 error comes when you run a class file created using Java 1.7 (major version 51.0) into a lower JRE version e.g. JRE 6, 5, or 4. There are two ways to solve this problem, first make sure you run your Java program in the same or higher version of JRE, on which it has compiled, and second use the cross-compilation option to create a class file compatible to a lower JRE. You can use javac -target option to create a class file for all JRE up-to-the JDK versions you are using for compilation.

For example, if you are compiling your Java source file into JDK 1.7 then you can create class files to run on JDK 1.1, 1.2, 1.3, 1.4, 1.5, and 1.6 version, but you cannot create class files compatible with Java 1.8.

When you compile a Java source file, it creates a class file and adds the class file version into it. In order to run that class file, your JRE must understand that version.

When JRE or JVM which is running the class doesn't able to understand the class file version they throw java.lang.UnsupportedClassVersionError: XXX : Unsupported major.minor version 51.0 error, where XXX is the name of your class which has an incompatible version.

Solution 1 - Upgrade JRE

Simplest solution to fix Unsupported major.minor version 51.0 error changes your Java virtual machine. Upgrade JRE to get a version the same or higher than the version you have used for compiling your project. Once you got that, just run your program again. It should work fine this time.

Since java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0 clearly says that this file needs, at least, Java 7 or higher to run. You need to install 32-bit or 64-bit JDK 1.7 or JRE 1.7 version depending upon whether you are running on 32-bit or 64-bit OS like If you are running on Windows 7 or Windows 8 then install a 64-bit version of JDK. You would also be fine if you are running on Java 8, but don't run the program on Java 6 or lower version.

Solution 2 - Use javac -target option

As you know now that root cause of any java.lang.UnsupportedClassVersionError: Unsupported major.the minor version is incompatible JRE version at run-time, but changing JRE is not the only solution you have, you can even compile your class file for lower JRE version. In order to adapt this solution, you need to re-compile your project.

If recompilation is an option then make sure you add -target option to javac with the corresponding Java version you are looking at as target runtime. For example, if you are generating a class file to run on Java 6, you can pass the following argument to the javac command :

javac -target 1.6 *.java

*.java is to compile all Java files and target 1.6 means the class file will be compatible with Java SE 6 JVM. If you want to compile a class file for Java 5, just use -target 1.5 option. See Core Java Volume 1  by Cay S. Horstmann for more detail on the Java compiler.

Unsupported major.minor version 51.0 error in Java [Solved]

How to fix an Unsupported major.minor version 51.0 in Eclipse

If you are getting an Unsupported major.minor version 51.0 error while running your project in Eclipse, then you need to change the compiler option for your project in Eclipse. This actually happens when you have a project-specific compiler version setting but running on Eclipse's default JRE. If you later changed Eclipse default JRE from JDK 1.7 to JDK 1.6, you will see the following error :

java.lang.UnsupportedClassVersionError: dto/ReverseArrayInPlace :
 Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(
    at java.lang.ClassLoader.defineClass(
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
Exception in thread "main" 

You can see follow following steps to solve the Unsupported major.minor version 51.0 error in Eclipse IDE:

1. Select project, right-click, select properties

2. Select Java Compiler option, as shown below, you will see that JDK 1.7 was selected by default

java.lang.UnsupportedClassVersionError: : Unsupported major.minor version 51.0 solution

3. Check the checkbox "Enable project specific settings"

4. Choose the Compiler compliance level you want, as shown below :

How to fix Unsupported major.minor version 51.0 in Java

Choose 1.6 if you want to run your program on Java 6 JVM, choose 1.7 if you want to execute your Java program with JRE 1.7, etc.

Alternatively, you can also change the JRE version in Run Configuration by doing the following steps :

1. Click on Run Menu and then choose Run Configurations as shown below

2. Choose your Java Application from left side list of Java application which has run configuration

How to find on which JRE version your program in running Eclipse

3. Select the JRE tab, if you are already getting this error then you would see the red cross on the tab, warning you about a mismatch JRE version.

4. Just select the right JRE version to run your program i.e. if you are compiling using JDK 7 then choose Java 7 here.
How to change JRE version in Eclipse

Once you make this change, you can just run your Java application and it should run fine, without any sign of java.lang.UnsupportedClassVersionError: Unsupported major.minor version 51.0 error.

That's all about how to solve the Unsupported major.minor version 51.0 error in Java and, in particular, Eclipse IDE. Once you know the root cause it becomes easy to solve the problem, it's always a mismatch between the Java version at compile and run-time.

You can also get an unsupported major.minor version 51.0 error from your build tools like Maven and ANT. Since they internally use the javac compiler to compile the file, you need to customize the corresponding task e.g. in ANT, there is a javac task to compile a source file, just provide a target parameter to it to solve this problem.

Related debugging tutorials

If you like this tutorial and wants to learn more about troubleshooting errors and exception in Java, just check some of my earlier tutorials and debugging guide. You will learn the root cause of many common Java problems and tips to solve them :
  • How to fix Unsupported major.minor version 52.0 error in Java [solution]
  • Failed to connect to Queue using WebSphere MQ Series error [solution]
  • How to fix java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver erorr [solution]
  • Solution of JDBC error java.lang.ClassNotFoundException: org.postgresql.Drive [solution]
  • How to deal with java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver? [solution]
  • Root cause of  java.lang.ClassNotFoundException: com.mysql.jdbc.Drive error in Java? [solution]
  • Cause and solution of java.sql.SQLException: No suitable driver : sqljdbc4.jar [solution]
  • Root cause java.lang.ClassNotFoundException: in Java? [analysis]


Unknown said...

Use javac -target option can make compile successful but at run time I still get the unsupported version error

javin paul said...

check the JRE which is used to running your code and confirm it can support the version of class files you have.

Unknown said...

Hi i get this error while execting the comman mvn -version. Can anyone please help in this

javin paul said...

hello Unknown, which version of maven are you using and which version of Java are you using? check mvn --version and java -version and paste it here

Post a Comment