我如何让 Eclipse 为 Java 使用不同的编译器版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2540548/
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
how do I get eclipse to use a different compiler version for Java?
提问by user26270
It seems like this should be a simple task, with the options in the Preferences menu for different JREs and the ability to set different compiler and build paths per project. However, it also seems to simply not work.
看起来这应该是一个简单的任务,具有不同 JRE 的首选项菜单中的选项以及为每个项目设置不同编译器和构建路径的能力。但是,它似乎也根本不起作用。
For example, I have my JAVA_HOME set to a jre for Java 1.6. It's still not clear to me how Eclipse uses this, but it appears to be defaulting to this and not taking the project overrides. I have also installed Java 1.5, and added a JRE for this in eclipse in the Java->Installed JREs section.
例如,我将我的 JAVA_HOME 设置为 Java 1.6 的 jre。我仍然不清楚 Eclipse 是如何使用它的,但它似乎默认为这个并且没有覆盖项目。我还安装了 Java 1.5,并在 Eclipse 中的 Java->Installed JREs 部分为此添加了一个 JRE。
In my project, I've set the compiler compliance level to 1.5. In the build path for the project, I've added the System Library for the Java 1.5 JRE. However, I'm getting compile errors for a class that implements PreparedStatement for not implementing abstract methods that only exist in Java 1.6 PreparedStatement. Specifically, the methods
在我的项目中,我将编译器合规性级别设置为 1.5。在项目的构建路径中,我添加了 Java 1.5 JRE 的系统库。但是,我收到了一个实现 PreparedStatement 的类的编译错误,因为它没有实现只存在于 Java 1.6 PreparedStatement 中的抽象方法。具体来说,方法
setAsciiStream(int, InputStream, long) and
setAsciiStream(int, InputStream)
Strangely enough, it worked when we were compiling it against Java 1.4, which it was originally written for. We added the JREs for Java 1.4 and referenced that system library in the project, and set the project's compiler level to 1.4, and it works fine. But when I do the same changes to try to point to Java 1.5, it instead uses 1.6.
奇怪的是,当我们针对它最初编写的 Java 1.4 编译它时,它起作用了。我们为 Java 1.4 添加了 JRE 并在项目中引用了该系统库,并将项目的编译器级别设置为 1.4,它工作正常。但是当我进行相同的更改以尝试指向 Java 1.5 时,它改为使用 1.6。
Any ideas why?
任何想法为什么?
回答by Pops
First off, are you setting your desired JRE or your desired JDK?
首先,您是设置所需的 JRE 还是所需的 JDK?
Even if your Eclipse is set up properly, there might be a wacky project-specific setting somewhere. You can open up a context menu on a given Java project in the Project Explorer and select Properties > Java Compiler to check on that.
即使您的 Eclipse 设置正确,某处也可能有一个古怪的项目特定设置。您可以在 Project Explorer 中打开给定 Java 项目的上下文菜单,然后选择 Properties > Java Compiler 进行检查。
If none of that helps, leave a comment and I'll take another look.
如果这些都没有帮助,请发表评论,我再看看。
回答by Edwin Buck
From the menu bar: Project -> Properties -> Java Compiler
从菜单栏:项目 -> 属性 -> Java 编译器
Enable project specific settings (checked) Uncheck "use Compliance from execution environment '.... Select the desired "compiler compliance level"
启用项目特定设置(选中)取消选中“从执行环境使用合规性”....选择所需的“编译器合规性级别”
That will allow you to compile "1.5" code using a "1.6" JDK.
这将允许您使用“1.6”JDK 编译“1.5”代码。
If you want to acutally use a 1.5 JDK to produce "1.5" compliant code, then install a suitable 1.5 JDK and tell eclipse where it is installed via:
如果你想真正使用 1.5 JDK 来生成“1.5”兼容代码,那么安装一个合适的 1.5 JDK 并通过以下方式告诉 eclipse 它的安装位置:
Window -> preferences -> Installed JREs
窗口 -> 首选项 -> 已安装的 JRE
And then go back to your project
然后回到你的项目
Project -> properties -> Java Build Path -> libraries
项目 -> 属性 -> Java 构建路径 -> 库
remove the 1.6 system libaries, and: add library... -> JRE System LIbrary -> Alternate JRE -> The JRE you want.
删除 1.6 系统库,然后:添加库... -> JRE 系统库 -> 备用 JRE -> 你想要的 JRE。
Verify that the correct JRE is on the project's build path, save everything, and enjoy!
验证正确的 JRE 位于项目的构建路径上,保存所有内容,然后享受吧!
回答by bcoughlan
Just to clarify, do you have JAVA_HOME set as a system variable or set in Eclipse classpath variables? I'm pretty sure (but not totally sure!) that the system variable is used by the command line compiler (and Ant), but that Eclipse modifies this accroding to the JDK used
只是为了澄清,您是否将 JAVA_HOME 设置为系统变量或在 Eclipse 类路径变量中设置?我很确定(但不完全确定!)命令行编译器(和 Ant)使用了系统变量,但是 Eclipse 根据使用的 JDK 修改了这个
回答by Kevin Swann
Eclipse uses it's own internal compiler that can compile to several Java versions.
Eclipse 使用它自己的内部编译器,可以编译为多个 Java 版本。
From Eclipse Help > Java development user guide > Concepts > Java Builder
从 Eclipse 帮助 > Java 开发用户指南 > 概念 > Java Builder
The Java builder builds Java programs using its own compiler (the Eclipse Compiler for Java) that implements the Java Language Specification.
Java 构建器使用它自己的编译器(Java Eclipse 编译器)来构建 Java 程序,该编译器实现了 Java 语言规范。
For Eclipse Mars.1 Release (4.5.1), this can target 1.3 to 1.8 inclusive.
对于 Eclipse Mars.1 Release (4.5.1),这可以针对 1.3 到 1.8(含)。
When you configure a project:
配置项目时:
[project-name] > Properties > Java Compiler > Compiler compliance level
This configures the Eclipse Java compiler to compile code to the specified Java version, typically 1.8 today.
这将配置 Eclipse Java 编译器以将代码编译为指定的 Java 版本,目前通常为 1.8。
Host environment variables, eg JAVA_HOME etc, are not used.
不使用主机环境变量,例如 JAVA_HOME 等。
The Oracle/Sun JDK compiler is not used.
不使用 Oracle/Sun JDK 编译器。