关于jdk1.6:java.lang.UnsupportedClassVersionError不支持的major.minor版本51.0

java.lang.UnsupportedClassVersionError Unsupported major.minor version 51.0

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
unsupported major .minor version 51.0

我正试图构建一个应用程序,但它给出了一些错误。我的JDK版本如下:

1
2
3
java version"1.6.0_30"
Java(TM) SE Runtime Environment (build 1.6.0_30-b12)
Java HotSpot(TM) Client VM (build 20.5-b03, mixed mode, sharing)

这是我的错误日志:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Buildfile: build.xml

compile:
     [echo] Compiling the service....
     [echo]   ...mkdir for classes first....
     [echo]   ...java classes next....
     [echo]   ...mkdir for generated code next....
     [echo]  ...generate stubs/skeletons next....
[wscompile] command line: wscompile -d /home/vivekray/program/MTrans/WEB-INF/classes -features:wsi,documentliteral -gen:server -keep -model /home/vivekray/program/MTrans/WEB-INF/model.gz -verbose -Xprintstacktrace /home/vivekray/program/MTrans/src/config.xml -classpath /home/vivekray/program/MTrans/WEB-INF:/home/vivekray/program/MTrans/WEB-INF/classes:/home/vivekray/program/MTrans/lib/log4j-1.2.16.jar:/home/vivekray/jwsdp-2.0/jwsdp-shared/lib/mail.jar:/home/vivekray/jwsdp-2.0/jwsdp-shared/lib/activation.jar:/home/vivekray/jwsdp-2.0/jaxp/lib/jaxp-api.jar:/home/vivekray/jwsdp-2.0/jaxp/lib/endorsed/dom.jar:/home/vivekray/jwsdp-2.0/jaxp/lib/endorsed/sax.jar:/home/vivekray/jwsdp-2.0/jaxp/lib/endorsed/xalan.jar:/home/vivekray/jwsdp-2.0/jaxp/lib/endorsed/xercesImpl.jar:/home/vivekray/jwsdp-2.0/jaxrpc/lib/jaxrpc-api.jar:/home/vivekray/jwsdp-2.0/jaxrpc/lib/jaxrpc-spi.jar:/home/vivekray/jwsdp-2.0/jaxrpc/lib/jaxrpc-impl.jar:/home/vivekray/jwsdp-2.0/saaj/lib/saaj-api.jar:/home/vivekray/jwsdp-2.0/saaj/lib/saaj-impl.jar:/home/vivekray/jwsdp-2.0/jwsdp-shared/lib/relaxngDatatype.jar:/home/vivekray/jwsdp-2.0/jwsdp-shared/lib/xsdlib.jar:/home/vivekray/jwsdp-2.0/jwsdp-shared/lib/jax-qname.jar:/home/vivekray/jwsdp-2.0/apache-ant/lib/ant.jar:/home/vivekray/jwsdp-2.0/fastinfoset/lib/FastInfoset.jar:/home/vivekray/Softwares/apache-tomcat-7.0.27/lib/servlet-api.jar
[wscompile] [creating model: mtransservice]
[wscompile] [creating service: Mtransservice]

BUILD FAILED
java.lang.UnsupportedClassVersionError: in/cdac/mobile/computing/mtrans/MtransService : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at org.apache.tools.ant.AntClassLoader.defineClassFromData(AntClassLoader.java:1146)
    at org.apache.tools.ant.AntClassLoader.getClassFromStream(AntClassLoader.java:1324)
    at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1388)
    at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1341)
    at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:1088)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:295)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at com.sun.xml.rpc.processor.modeler.rmi.RmiUtils.getLoadableClassName(RmiUtils.java:78)
    at com.sun.xml.rpc.processor.modeler.rmi.RmiUtils.getRealName(RmiUtils.java:54)
    at com.sun.xml.rpc.processor.modeler.rmi.RmiModeler.modelPort(RmiModeler.java:676)
    at com.sun.xml.rpc.processor.modeler.rmi.RmiModeler.buildLiteralModel(RmiModeler.java:603)
    at com.sun.xml.rpc.processor.modeler.rmi.RmiModeler.buildModel(RmiModeler.java:457)
    at com.sun.xml.rpc.processor.config.ModelInfo.buildModel(ModelInfo.java:88)
    at com.sun.xml.rpc.processor.Processor.runModeler(Processor.java:63)
    at com.sun.xml.rpc.tools.wscompile.CompileTool.run(CompileTool.java:714)
    at com.sun.xml.rpc.util.ToolBase.run(ToolBase.java:43)
    at com.sun.xml.rpc.tools.ant.Wscompile.execute(Wscompile.java:746)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
    at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:357)
    at org.apache.tools.ant.Target.performTasks(Target.java:385)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
    at org.apache.tools.ant.Main.runBuild(Main.java:758)
    at org.apache.tools.ant.Main.startAnt(Main.java:217)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)

Total time: 1 second

这些人给了你失败的原因,但没有告诉你如何解决。即使您有一个JDK与您正在尝试的JVM相匹配,这个问题也可能出现。

Projt>属性-> Java编译器

启用项目特定的设置。

然后选择编译器兼容级别为1.6或1.5,构建并测试应用程序。

现在应该没事了。


java.lang.UnsupportedClassVersionError的发生是因为编译时JDK较高,运行时JDK较低。

以下是版本列表:

1
2
3
4
5
6
7
8
9
Java SE 9 = 53,
Java SE 8 = 52,
Java SE 7 = 51,
Java SE 6.0 = 50,
Java SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45


版本51是Java 7,您可能使用了错误的JDK。检查java_主页。

如前所述,在二进制兼容性下


我遇到了同样的问题,当jdk 1.7被用于编译时,jre1.4被用于执行。

我的解决方案是通过在现有路径设置之前添加路径名c:glassfish3jdk7in来设置环境变量路径。更新后的值为"c:glassfish3jdk7in;c:sunsdkin"。更新之后,问题就消失了。


这意味着您在一个特定的JDK下编译类,然后尝试在旧版本的JDK下运行它们。


使用Maven并使用Maven编译器插件显式调用实际的正确版本JDK JavaC.EXE命令,因为Maven可以运行任何版本;这也捕捉了Javac中的愚蠢的长期错误,它在编译早期Java JAVE时没有发现运行时中断类版本JAR和丢失类/方法/属性。艾斯!后面的部分可以通过在新的类、方法和属性中添加版本属性,或者单独的编译器版本控制数据来很容易地在Java 1.5 +中被固定,所以Sun和Oracle是一个非常愚蠢的疏忽。


确保在编译/运行时使用了正确的SDK,并且确保使用了source/target 1.7。