I run a jar file on an external server. the jar-file is compiled by mvn on my local machine. the external server has java 1.7 installed, so I needed to explicitly tell maven to compile to java 1.7 because on my local machine I already use java 1.8.
However, I still get the following error when running the jar:
Exception in thread "main" java.lang.UnsupportedClassVersionError: io/package/name/Launcher : 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)
Could not find the main class: io.package.name.Launcher. Program will exit.
According to other threads Unsupported major.minor version 51.0
means that java 7 is expected (update: this is wrong. actually the error means that the system where I try to execute the jar can not handle the provided version which is 51 which equals java 7 / 1.7. so this implies that the system runs an even lower version (java 1.6?)). However, I double checked the compiled classes and they indeed should be compatible to java 7. but maybe I did something wrong when checking. I checked by using a HexEditor as explained here: how to check the jdk version used to compile a .class file
Here is a screenshot showing the 7th and 8th byte of Launcher.class. The 0x33
is 51. And 51 means that the class file is compatible with Java 7 (according to https://stackoverflow.com/a/1096159/973158).
A java -version
on the server yields:
java version "1.7.0_79"
OpenJDK Runtime Environment (rhel-2.5.5.1.el6_6-x86_64 u79-b14)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)
And this is how I tell maven to compile to java 1.7 in pom.xml
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
What am I doing wrong? Could it be that there is some sort of cache on the server which still holds an old version of the class which was compiled to java 1.8?
FINAL UPDATE:
my code actually was java 1.7 compatible, however, I wasn't aware of the fact that java 1.6 was required. my bad.