如何让eclipse使用不同的Java编译器版本?

时间:2023-01-25 09:00:22

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,并在Java->安装的JREs部分中在eclipse中添加了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添加了JREs,并在项目中引用了系统库,并将项目的编译器级别设置为1.4,而且效果很好。但是当我做同样的改变试图指向Java 1.5时,它使用了1.6。

Any ideas why?

任何想法为什么?

5 个解决方案

#1


42  

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

从菜单栏:Project ->属性-> 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

窗口->偏好->安装了JREs。

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系统libaries,并:添加库…-> JRE系统库->替代JRE ->你想要的JRE。

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

验证项目的构建路径上是否有正确的JRE,保存所有内容并享受!

#2


2  

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编译器来检查它。

If none of that helps, leave a comment and I'll take another look.

如果没有任何帮助,请留下评论,我会再看一遍。

#3


2  

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 builder使用它自己的编译器(Java的Eclipse编译器)来构建Java程序,它实现了Java语言规范。

For Eclipse Mars.1 Release (4.5.1), this can target 1.3 to 1.8 inclusive.

对于Eclipse Mars.1版本(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编译器。

#4


1  

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会修改这个accroding到JDK的使用。

#5


0  

Another approach for setting the JDK 1.8 compiler with Maven is using this plugin:

使用Maven来设置JDK 1.8编译器的另一种方法是使用这个插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
        <verbose>true</verbose>
        <fork>true</fork>
        <executable>C:\Program Files\Java\jdk1.8.0_102\bin\javac</executable>
        <source>1.8</source>
        <target>1.8</target>
     </configuration>
 </plugin>

#1


42  

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

从菜单栏:Project ->属性-> 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

窗口->偏好->安装了JREs。

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系统libaries,并:添加库…-> JRE系统库->替代JRE ->你想要的JRE。

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

验证项目的构建路径上是否有正确的JRE,保存所有内容并享受!

#2


2  

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编译器来检查它。

If none of that helps, leave a comment and I'll take another look.

如果没有任何帮助,请留下评论,我会再看一遍。

#3


2  

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 builder使用它自己的编译器(Java的Eclipse编译器)来构建Java程序,它实现了Java语言规范。

For Eclipse Mars.1 Release (4.5.1), this can target 1.3 to 1.8 inclusive.

对于Eclipse Mars.1版本(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编译器。

#4


1  

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会修改这个accroding到JDK的使用。

#5


0  

Another approach for setting the JDK 1.8 compiler with Maven is using this plugin:

使用Maven来设置JDK 1.8编译器的另一种方法是使用这个插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
        <verbose>true</verbose>
        <fork>true</fork>
        <executable>C:\Program Files\Java\jdk1.8.0_102\bin\javac</executable>
        <source>1.8</source>
        <target>1.8</target>
     </configuration>
 </plugin>