Java 尝试引用 Jar 文件时,Eclipse 出现不受支持的 Major.minor 版本 51.0 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18878045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unsupported major.minor version 51.0 Error With Eclipse When Trying to Reference Jar File
提问by James Oravec
I'm compiling a jar file targeted for java version 6, however I'm getting an error related to Unsupported major.minor version 51.0
. After reading several posts, such as How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor versionI was not able to solve my issue and took extreme steps to try to isolate the issue, as described below.
我正在编译一个针对 Java 版本 6 的 jar 文件,但是我收到了一个与Unsupported major.minor version 51.0
. 在阅读了几篇文章后,例如How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version我无法解决我的问题,并采取极端步骤尝试隔离问题,如下所述。
I created a java sandbox project whose purpose is to test the Xerces-For-Android jar that I'm making from source code and targeting to version 6. If I put the source code directly into the project, I'm able to compile and run my test program successfully. However if I make a jar file, then add that into my build path I get the Unsupported major.minor version 51.0
.
我创建了一个 java 沙盒项目,其目的是测试我从源代码制作的 Xerces-For-Android jar 并定位到版本 6。如果我将源代码直接放入项目中,我能够编译和成功运行我的测试程序。但是,如果我制作一个 jar 文件,然后将其添加到我的构建路径中,我会得到Unsupported major.minor version 51.0
.
Reading several posts, the Unsupported major.minor version 51.0
seems like it is an issue based on compilation of different java versions. The version 51.0 looks to be java 7. To simply my testing, I uninstalled all Java 7 installs from my machine, ensured that my eclipse projects point to the same Java 6 JRE, and that my JAVA_HOME is set to Java 6u45. I also restart eclipse to make sure my changes were in place.
看了几篇文章, Unsupported major.minor version 51.0
似乎是基于不同java版本编译的问题。版本 51.0 看起来是 java 7。为了简单的测试,我从我的机器上卸载了所有 Java 7 安装,确保我的 eclipse 项目指向相同的 Java 6 JRE,并且我的 JAVA_HOME 设置为 Java 6u45。我还重新启动 eclipse 以确保我的更改到位。
I'm using Ant
to create the jar file. The code is very simple and I even specify the javac target to be 1.6.
我正在使用Ant
创建 jar 文件。代码非常简单,我什至将 javac 目标指定为 1.6。
<project name="XercesForAndroid" default="dist" basedir=".">
<description>
Builds jar for Xerces-For-Android
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init" description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac target="1.6" srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the .jar file -->
<jar jarfile="${dist}/lib/Xerces-For-Android.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
After I run the ant script, I inspect the jar with 7-zip and find the manifest shows 6u45 was used to make it.
运行 ant 脚本后,我用 7-zip 检查 jar 并发现清单显示 6u45 用于制作它。
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_45-b06 (Sun Microsystems Inc.)
Any ideas on what I could be missing? I don't see how Java 7 could be referenced any more, but its seems like it is based on the 51.0 reference...
关于我可能缺少什么的任何想法?我看不到如何再引用 Java 7,但它似乎基于 51.0 引用...
A couple other side notes:
一些其他方面的说明:
- I manually deleted the .jar file and did a clean on the Xerces-For-Android project to ensure no old binaries were laying around.
- I also did a clean on my sandbox project for testing the use of the jar file. Same issue.
- After the uninstalls of Java 7, I did a reinstall of java 6u45. Still no luck with getting past the 51.0 reference.
- As per Joop Eggen's request, I verified the compiler settings in eclipse:
- 我手动删除了 .jar 文件并对 Xerces-For-Android 项目进行了清理,以确保没有旧的二进制文件。
- 我还对我的沙箱项目进行了清理,以测试 jar 文件的使用。同样的问题。
- 卸载 Java 7 后,我重新安装了 Java 6u45。通过 51.0 参考仍然没有运气。
- 根据 Joop Eggen 的要求,我在 eclipse 中验证了编译器设置:
The solution:
解决方案:
As per Joop Eggen's request, I ran the ant script in verbose mode (ant -verbose > build.log
)... This gave me more information to identify the problem, which was the fact that the "clean" task of ant
was not running, which is different from the "project clean" in eclipse. The manifest file in the jar was being updated, but the class files were not being recreated as they showed as "up to date". After adding the clean task as a dependency to the compile task, everything worked as expected. Leaving this post in the off chance someone has a similar issue.
根据 Joop Eggen 的要求,我以详细模式 ( ant -verbose > build.log
)运行了 ant 脚本......这为我提供了更多信息来识别问题,即“干净”任务ant
没有运行,这与“项目清洁”在 Eclipse 中。jar 中的清单文件正在更新,但类文件没有被重新创建,因为它们显示为“最新”。在将 clean 任务作为依赖项添加到 compile 任务之后,一切都按预期进行。如果有人遇到类似问题,就不要再写这篇文章了。
采纳答案by Joop Eggen
Disable automatic compilation in eclipse, or better run ant from the command line. And do <javac verbose="true" ...>
. Check whether there is a JAVA_HOME set and the Ant installation.
在 Eclipse 中禁用自动编译,或者更好地从命令行运行 ant。并做<javac verbose="true" ...>
。检查是否有JAVA_HOME设置和Ant安装。
Javac also has compiler
and source
attributes.
Javac 也有compiler
和source
属性。
Maybe be paranoid and inspect the .class files, for code see dumpJavaClassVersion.
也许偏执并检查 .class 文件,有关代码,请参阅dumpJavaClassVersion。
回答by rolfl
Make sure you do a full clean of everything before you do the build. The major.Minor thing happens for classes in the Jar, not necessarily for the jar itself.
在进行构建之前,请确保对所有内容进行全面清理。Major.Minor 事件发生在 Jar 中的类上,不一定发生在 jar 本身。
Go through and remove any .class files you can find, and recompile from scratch.
仔细检查并删除您能找到的所有 .class 文件,然后从头开始重新编译。
回答by Deepti Kohli
If you are using maven to build your project, see below. Ensure that you specify source and target accordingly. I had this error when I compiled in 1.7 (kept both 1.7) but found that on amazon beanstalk, it was 1.6
如果您使用 maven 构建您的项目,请参见下文。确保相应地指定源和目标。我在 1.7 中编译时出现此错误(同时保留 1.7)但发现在 amazon beanstalk 上,它是 1.6
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>