Maven运行异常:Exception in thread "main" java.lang.UnsupportedClassVersionError

时间:2022-10-12 00:16:41

转载请注明来源:http://blog.csdn.net/loongshawn/article/details/50556760

1 背景

Linux平台部署maven项目,提示如下异常:

Exception in thread “main”java.lang.UnsupportedClassVersionError: com/autonavi/App : Unsupported major.minor version 51.0

2 异常

异常摘要

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/autonavi/App : Unsupported major.minor version 51.0

详细异常

$sudo ./appctl.sh start
/home/admin/rus-api-services/target/rus-api-services
start com.autonavi.App
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/autonavi/App : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
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:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: com.autonavi.App. Program will exit.
the running process:

3 排查

检查Linux服务器的java版本

$java -version
java version "1.6.0_22"
OpenJDK Runtime Environment (IcedTea6 1.10.4) (rhel-1.41.1.10.4.1.alios6-x86_64)
OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)

检查Linux服务器的javac版本

$javac -version
javac 1.6.0_22

服务器java版本是一致的,没有问题,然后检查maven项目所要求的java版本,进入POM文件中查看:

<properties>
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.autonavi.App</start-class>
</properties>

原来maven项目要求jdk是1.7的,而服务器是1.6的,两者不匹配才导致上述异常。

4 解决

将maven项目所要求的java版本改成1.6,即POM文件如下:

<properties>
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.autonavi.App</start-class>
</properties>

之后maven项目运行正常。