Thursday 2 June 2016

How to solve Unsupported major.minor version 51.0

As per the javadoc, UnsupportedClassVersionError thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbers in the file are not supported.

When UnsupportedClassVersionError occurs?
When you compile a class using higher jdk version (like Java8) and trying to run using lower jdk (like Java7), you will get this error.
Let me give an example,


HelloWorld.java
public class HelloWorld{
 public static void main(String args[]){
  System.out.println("Hello World");
 }
}


Let me compile above program using Java8 compiler and try to run using Java7 virtual machine.
C:\java_programs>"C:\Program Files\Java\jdk1.8.0_45\bin\javac" HelloWorld.java

C:\java_programs>"C:\Program Files\Java\jdk1.7.0_79\bin\java" HelloWorld
Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)


As you see the output, Java run time throws “java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 52.0”.

What is 52.0 here?
When you compile a Java class file, Java embed the major version number of the class file format being used. Java run time environment checks this number before running. If this number is greater than your java run time environment, you will get "Unsupported major.minor version" error.

Following table summarizes the major version numbers for different java run time environments.

Java Version
Major version number
Java SE 9
53 (0x35 hex)
Java SE 8
52 (0x34 hex)
Java SE 7
51 (0x33 hex)
Java SE 6.0
50 (0x32 hex)
Java SE 5.0
49 (0x31 hex)
JDK 1.4
48 (0x30 hex)
JDK 1.3
47 (0x2F hex)
JDK 1.2
46 (0x2E hex)
JDK 1.1
45 (0x2D hex)

How to resolve above error?
There are two ways.

Approach 1: One way is upgrade your Java environment to higher version.

Approach 2: Use javac -target option
‘javac –target’ generate class files for specific VM version.

"C:\Program Files\Java\jdk1.8.0_45\bin\javac" -source 1.7 -target 1.7 HelloWorld.java

Above statement compiles HelloWorld.java using java compiler 1.8 and set the target level to 1.7

C:\java_programs>"C:\Program Files\Java\jdk1.8.0_45\bin\javac" -source 1.7 -target 1.7 HelloWorld.java
warning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning

C:\java_programs>"C:\Program Files\Java\jdk1.7.0_79\bin\java" HelloWorld
Hello World


You may like

Reference



No comments:

Post a Comment