从 Eclipse 运行 Ant 时出现 UnsupportedClassVersionError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14581229/
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
UnsupportedClassVersionError when running Ant from eclipse
提问by Nitzan Tomer
I'm trying to run an ant script from eclipse but it fails with the following message:
我正在尝试从 eclipse 运行一个 ant 脚本,但它失败并显示以下消息:
BUILD FAILED
project_path/build.xml:5:
java.lang.UnsupportedClassVersionError: com/sun/tools/javac/Main :
Unsupported major.minor version 51.0
However, when I run this from command line (./ant) it runs as expected.
Environment info:
但是,当我从命令行 (./ant) 运行它时,它会按预期运行。
环境信息:
- OS: Linux Mint 64
- Eclipse: Juno (4.2.1)
- JDK: Oracle 1.7.0_11
- Ant (command line): 1.8.2
- 操作系统:Linux Mint 64
- 日食:朱诺 (4.2.1)
- JDK:Oracle 1.7.0_11
- Ant(命令行):1.8.2
I've also set the "Ant Home" (within eclipse settings) to be the same as the one I use from command line.
我还将“Ant Home”(在 eclipse 设置中)设置为与我从命令行使用的相同。
I've searched a lot on this issue but I can't seem to find anything that helped, I can try to uninstall all java (jdk/jre) I currently have installed, and then reinstall java 7, but I prefer not to take this route.
我在这个问题上搜索了很多,但我似乎找不到任何有帮助的东西,我可以尝试卸载我目前安装的所有 java (jdk/jre),然后重新安装 java 7,但我不想采取这条路线。
Any ideas? Thanks!
有任何想法吗?谢谢!
回答by Ian Roberts
UnsupportedClassVersionError: com/sun/tools/javac/Main : Unsupported major.minor version 51.0
UnsupportedClassVersionError:com/sun/tools/javac/Main:不支持的major.minor 版本51.0
This suggests there's some mix up between versions - Ant is trying to run the Java 7 javac
on a Java 6 JVM. Have a look at your project properties, builders, Ant Builder, and check the classpath section. On my Eclipse with a newly-created Ant Builder I have the tools.jar from my 1.7 JDK listed under user entries -> additional tasks and support. If you have the same, but your Eclipse is itself running on Java 6 then it won't work. You can find out which Java Eclipse itself is running on via the "about Eclipse" box under installation details -> configuration. This may not be the same as the version of Java that your project is configured to use.
这表明版本之间存在一些混淆——Ant 试图javac
在 Java 6 JVM 上运行 Java 7 。查看您的项目属性、构建器、Ant Builder,并检查类路径部分。在带有新创建的 Ant Builder 的 Eclipse 上,我的 1.7 JDK 中的 tools.jar 列在用户条目 -> 附加任务和支持下。如果你有同样的东西,但你的 Eclipse 本身运行在 Java 6 上,那么它就不会工作。您可以通过安装详细信息 -> 配置下的“关于 Eclipse”框找出运行的是哪个 Java Eclipse。这可能与您的项目配置使用的 Java 版本不同。
Another thing that might fix it is to go to the JRE tab in the ant builder settings and tell it to run in a separate JRE rather than running within the Eclipse process.
另一个可能修复它的方法是转到 ant builder 设置中的 JRE 选项卡,并告诉它在单独的 JRE 中运行,而不是在 Eclipse 进程中运行。
回答by Anders R. Bystrup
Your Eclipse ant task is trying to run the Java7 JDK compiler using a pre-Java7 JDK/JRE. Make sure you use a Java7 JRE to run Eclipse.
您的 Eclipse ant 任务正在尝试使用 Java7 之前的 JDK/JRE 运行 Java7 JDK 编译器。确保您使用 Java7 JRE 来运行 Eclipse。
回答by Avinash Nair
generally if you compile Java program in Java 1.7 it will create class file with major version 51.0 and if you run that on lower java version you will get
通常,如果您在 Java 1.7 中编译 Java 程序,它将创建主要版本为 51.0 的类文件,如果您在较低的 Java 版本上运行它,您将获得
java.lang.unsupportedclassversionerror unsupported major.minor version 51.0
check thispost for more details on the same
查看此帖子以获取有关相同内容的更多详细信息