如何让eclipse为Java使用不同的编译器版本? [英] how do I get eclipse to use a different compiler version for Java?

查看:157
本文介绍了如何让eclipse为Java使用不同的编译器版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎这应该是一个简单的任务,在首选项菜单中有不同JRE的选项,并且能够为每个项目设置不同的编译器和构建路径。但是,它似乎根本不起作用。



例如,我的JAVA_HOME设置为Java 1.6的jre。我仍然不清楚Eclipse如何使用它,但它似乎是默认的,而不是使项目覆盖。我还安装了Java 1.5,并在Java-> Installed JREs部分的eclipse中添加了一个JRE。



在我的项目中,我设置了编译器的合规性水平到1.5。在项目的构建路径中,我添加了Java 1.5 JRE的系统库。但是,为了不实现仅存在于Java 1.6 PreparedStatement中的抽象方法,我正在获得一个实现PreparedStatement的类的编译错误。具体来说,方法

  setAsciiStream(int,InputStream,long)和
setAsciiStream(int,InputStream)$ b $奇怪的是,当我们针对原来为之编写的Java 1.4进行编译时,它工作得很好。我们添加了Java 1.4的JRE,并在项目中引用了该系统库,并将项目的编译器级别设置为1.4,并且工作正常。但是,当我做同样的改变来尝试指向Java 1.5时,它会使用1.6。



任何想法为什么?

解决方案

从菜单栏:
项目 - >属性 - > Java编译器



启用项目特定设置(检查)
取消选中执行环境中使用合规性....
选择所需的编译器合规级别



这将允许您使用1.6JDK编译1.5代码。



如果要使用1.5 JDK生成1.5兼容代码,则安装合适的1.5 JDK并通过以下方式告诉eclipse在哪里安装:



窗口 - >首选项 - >已安装的JRE



然后返回到您的项目



项目 - >属性 - > Java构建路径 - >库



删除1.6系统库和
添加库... - > JRE系统LIBRARY - >替代JRE - >您想要的JRE。



验证正确的JRE是在项目的构建路径上,保存一切,并享受!


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.

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.

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

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.

Any ideas why?

解决方案

From the menu bar: Project -> Properties -> Java Compiler

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.

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:

Window -> preferences -> Installed JREs

And then go back to your project

Project -> properties -> Java Build Path -> libraries

remove the 1.6 system libaries, and: add library... -> JRE System LIbrary -> Alternate JRE -> The JRE you want.

Verify that the correct JRE is on the project's build path, save everything, and enjoy!

这篇关于如何让eclipse为Java使用不同的编译器版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆