Java : Unsupported major.minor version 51.0

1. Problem

Run a “jar” on Debian 7.5, and hits the following error messages :


$ java -jar ripecrawler.jar
Exception in thread "main" java.lang.UnsupportedClassVersionError: 
	com/mkyong/whois/job/RipeCrawlJob : Unsupported major.minor version 51.0

	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:268)

A quick check on the Java version, shows the Debian is using 1.6


$ java -version
java version "1.6.0_31"
OpenJDK Runtime Environment (IcedTea6 1.13.3) (6b31-1.13.3-1~deb7u1)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)

2. Reason

This is because the “jar” is compiled with JDK 1.7, but you try to run in a JDK 1.6 environment. Refer to the following Wikipedia Java class reference.


J2SE 8 = 52 (0x34 hex)
J2SE 7 = 51 (0x33 hex)
J2SE 6.0 = 50 (0x32 hex)
J2SE 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)

P.S major.minor version 51.0 = JDK 1.7 / 7

For example, if you compiled your “jar” with JDK 1.6, and try to run in a JDK 1.5 environment, the error message will change to this :


Unsupported major.minor version 50.0

3. Solution

To fix it, download JDK 7, and change the default JDK from 1.6 to 1.7

3.1 Download and install openjdk-7-jdk


$ sudo apt-get install openjdk-7-jdk openjdk-7-jre

3.2 Change the default Java to 1.7 (Working on Debian like system only).


$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode
  2            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1051      manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 
to provide /usr/bin/java (java) in manual mode

$ java -version
java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (7u55-2.4.7-1~deb7u1)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)

Done.

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
trackback
How to change Tomcat to use JDK 7

[…] Java : Unsupported Major.Minor Version 51.0 […]

Truite
Guest
Truite

Thank you sir, that’s the quicker and better tutorial about this issue.

Suad D.
Guest
Suad D.

I am getting this problem even though I updated the java version to 1.7. Any tips?

Michael Sfakianos
Guest
Michael Sfakianos

Please make sure that both JAVA_HOME and PATH environment variables are updated to match your JAVA 1.7 directory paths.

Deepak Singh Negi
Guest
Deepak Singh Negi

Still the same… 🙁

Jeff
Guest
Jeff

Good Article..As always very clear and informative..
Also, want to add.. we can always re-compile that Jar with Jdk1.6 and will not run in to this issue.
we can always compile classes with Lower version of java (say 1.6) and run it on Higher version (say 1.7)..and it will run fine.. no issues.. but not other way around (compile with higher version and try to run on lower version).. we will get error..’unsupported major minor version)