Saturday, August 29, 2015

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 same or higher version of JRE, on which it has compiled, and second use 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 version 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 add 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 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 e.g. 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 corresponding Java version you are looking as target runtime. For example, if you are generating class file to run on Java 6, you can pass the following argument to 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 10th Edition by Cay S. Horstmann for more detail on Java compiler.

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

How to fix Unsupported major.minor version 51.0 in Eclipse

If you are getting Unsupported major.minor version 51.0 error while running your project in Eclipse, then you need to change 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 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 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 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 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 Java version at compile and run-time.  You can also get unsupported major.minor version 51.0 error from your build tool e.g. Maven and ANT. Since they internally use javac compiler to compile the file, you need to customize corresponding task e.g. in ANT, there is a javac task to compile source file, just provide target parameter to it to solve this problem.

Further Learning
Understanding the Java Virtual Machine: Class Loading and Reflection
Java Fundamentals, Part 1 and 2
Core Java for the Impatient - Covers Java SE 8

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 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]

No comments :

Post a Comment