In this tutorial, we will explain how to resolve the common error Unsupported major.minor version 51.0 that occurs when running a jar file on Debian 7.5. We have encountered this issue when attempting to run a jar file compiled with Java 7 on a system still using Java 6. Let’s walk through the error details and the steps to fix it.
Table of contents:
The Error in Action
When we run the jar file, we see the following error message:
$ 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 of the Java version on our Debian system reveals that we are using Java 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)
Understanding the Issue
The error occurs because the jar file was compiled with Java 7 (which uses major version 51) while our system is running Java 6 (which supports up to major version 50). This version mismatch leads to the Unsupported major.minor version 51.0 error.
For further details on Java class file versions, we refer to this comprehensive table that lists the major version numbers.
The Solution
To resolve this error, we need to install Java 7 and update our system’s default JDK from Java 6 to Java 7. Here is how we can do this:
-
Install Java 7
We install both the JDK and JRE for Java 7 using the following command:
$ sudo apt-get install openjdk-7-jdk openjdk-7-jre -
Update the Default Java Version
Next, we change the default Java version by running:
$ sudo update-alternatives --config javaThis command will display the available Java alternatives:
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 modeWe select option 2 to switch to Java 7.
-
Verify the Update
Finally, we verify that our system is now using Java 7:
$ 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)
Now, our jar file compiled with Java 7 will run smoothly on Debian 7.5.
Real-World Example
This approach is applicable in real-world scenarios where we work on Java projects across different environments. For example, if we are developing an application in Java 7 or later and then deploy it on a system that still uses an older Java version, following these steps ensures that our application runs without encountering version-related issues.
Conclusion
We have successfully resolved the Unsupported major.minor version 51.0 error by upgrading our system’s Java version from 6 to 7. This troubleshooting method not only applies to Debian 7.5 but can also be adapted to other Linux distributions facing similar Java version mismatches.
Thank you sir, that’s the quicker and better tutorial about this issue.
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)