简体   繁体   English

为什么eclipse不会将编译器切换到Java 8?

[英]Why won't eclipse switch the compiler to Java 8?

I checked out a Java project from SVN in eclipse and realized that it requires Java 8 because it uses lambdas etc. I installed the eclipse addon for Java 8 and restarted eclipse and and have the project set up like so: 我在eclipse中检查了一个来自SVN的Java项目,并意识到它需要Java 8,因为它使用了lambdas等。我为Java 8安装了eclipse插件并重新启动了eclipse,并且项目设置如下:

新Java项目

I noticed that near the bottom, it says that the default compiler compliance is 1.7, so I went into org.eclipse.jdt.core.prefs and set the compiler compliance variable to 1.8 , as per this answer . 我注意到接近底部,它表示默认的编译器合规性是1.7,所以我进入org.eclipse.jdt.core.prefs并将编译器合规性变量设置为1.8 ,根据这个答案 However, in Project -> Preferences -> Java Compiler, it still shows up as: 但是,在Project - > Preferences - > Java Compiler中,它仍然显示为:

请日食

I have set the JRE in Project -> Java Build Path: 我在Project - > Java Build Path中设置了JRE:

jre8

Yet the compiler refuses to compile lambda expressions - I get an error that looks like what I would get if I went ahead and typed it into Java 7. 然而编译器拒绝编译lambda表达式 - 我得到的错误看起来就像我继续将其输入到Java 7中所得到的错误。

This is the version of eclipse I'm using 这是我正在使用的eclipse版本

Version: Kepler Service Release 1
Build id: 20130919-0819

Is the only way to solve this to install a fresh version of Eclipse or am I missing something in the configuration? 解决此问题的唯一方法是安装新版本的Eclipse还是我在配置中遗漏了一些东西?

Two things: 两件事情:

First, JRE is not the same as the JDK. 首先,JRE与JDK不同。 If you do have the JDK, you need to configure eclipse to point to that in your settings. 如果你有JDK,你需要配置eclipse指向你设置中的那个。

Second, in your screenshot above, your compiler compliance level is set to 1.7. 其次,在上面的屏幕截图中,您的编译器合规性级别设置为1.7。 This will treat all your code as if it's using Java 1.7. 这会将您的所有代码视为使用Java 1.7。 Change this to 1.8 to fix your error. 将其更改为1.8以修复错误。

You will need to have Eclipse Luna in order to get support for Java 8, but you can add it to Kepler SR2 if you want. 您需要拥有Eclipse Luna才能获得对Java 8的支持,但如果需要,可以将其添加到Kepler SR2。 I'd try with Luna and the above suggestions before you go any further. 在你再继续之前,我会尝试使用Luna和上述建议。 See this reference . 请参阅此参考

Once you get Luna, your JAVA_HOME variable should be enough to get Eclipse to recognize JDK 8. If you want to specify an additional JDK, you can add a new Java System Library by going to: 一旦获得Luna,您的JAVA_HOME变量应足以让Eclipse识别JDK 8.如果要指定其他JDK,可以通过以下方式添加新的Java System Library:

Project -> Properties -> Java Build Path -> Libraries -> Add Library -> Java System Library

and navigating to a valid location for the JDK 8. 并导航到JDK 8的有效位置。

You can download your platform's JDK 8 here 您可以在此处下载平台的JDK 8

It cause eclipse kepler SR1 does not support new Java™ 8 language enhancements like lambda expression. 它导致eclipse kepler SR1不支持lambda表达式等新的Java™8语言增强功能。

From information here: http://www.eclipse.org/downloads/java8/ 从这里的信息: http//www.eclipse.org/downloads/java8/
I think you should use kepler SR2 with support plugin, or change to Eclipse Luna. 我认为您应该将kepler SR2与支持插件一起使用,或者更改为Eclipse Luna。


Updated link 16/09/2016: https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler 更新链接2016年6月16日: https//wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler

I had the same problem even though I had: 我有同样的问题,即使我有:

  • a freshly downloaded JDK 1.8.0 刚刚下载的JDK 1.8.0

  • JAVA_HOME is set JAVA_HOME已设置

  • java -version on command line reports 1.8 java -version在命令行上报告1.8

  • Java in control panel is set to 1.8 控制面板中的Java设置为1.8

  • downloaded Eclipse Mars 下载了Eclipse Mars

Eclipse only let me choose a compiler compliance level op to 1.7 in the compiler preferences, even though my installed JRE is 1.8.0. Eclipse只允许我在编译器首选项中选择编译器合规性级别op到1.7,即使我安装的JRE是1.8.0。 I also couldn't see a 1.8 in the Execution Environments underneath Installed JREs, only a JavaSE-1.7 (which I haven't even got installed!). 在安装的JRE下面的执行环境中,我也看不到1.8,只有JavaSE-1.7(我甚至没有安装它!)。 When I clicked on that, it shows "jdk1.8.0" as a compatible JRE, so I selected that, but still no change. 当我点击它时,它显示“jdk1.8.0”作为兼容的JRE,所以我选择了它,但仍然没有变化。

Then I unzipped Eclipse Mars into a brand new directory, created a new project, and now I can select 1.8, hurrah! 然后我将Eclipse Mars解压缩到一个全新的目录,创建了一个新项目,现在我可以选择1.8,欢呼! That greatly reduced the "Duplicate methods named spliterator..." errors I was getting when compiling my code under Java 1.8, however, there is still one left: 这大大减少了我在Java 1.8下编译代码时遇到的“重复方法命名为spliterator ...”错误,但仍有一个错误:

Duplicate default methods named spliterator with the parameters () and () are inherited from the types List and Set. 带有参数()和()的名为spliterator的重复默认方法继承自List和Set类型。

However, that's likely because I'm extending AbstractList and implementing Set, so I've fixed that for now by removing the implements Set because it doesn't really add anything in my case (other than signifying that my collection has only unique elements) 但是,这可能是因为我正在扩展AbstractList并实现Set,所以我现在通过删除实现Set来修复它,因为它在我的情况下并没有真正添加任何东西(除了表示我的集合只有唯一元素)

First of all you should get JdK 8. 首先你应该得到JdK 8。

if you have Jdk installed. 如果你安装了Jdk。

you should set its path using cmd prompt or system variables. 您应该使用cmd提示符或系统变量设置其路径。

sometimes it can happen that the path is not set due to which eclipse is unable to get the properties for jdk. 有时可能会发生路径未设置,因为eclipse无法获取jdk的属性。

Installing latest ecipse luna can solve your problem. 安装最新的ecipse luna可以解决您的问题。

i have indigo and luna. 我有靛蓝和月神。 i can set 1.8 in luna but 1.7 in indigo. 我可以在luna中设置1.8,在靛蓝中设置1.7。 Eclipse luna Eclipse月神

You can check the eclipse site. 你可以查看eclipse网站。 it says that the eclipse luna was certainly to associate the properties for jdk 8. 它说eclipse luna肯定会将jdk 8的属性关联起来。

You must install the JDT/Eclipse Java 8 Support For Kepler. 您必须安装JDT / Eclipse Java 8 Support for Kepler。 https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler

I had the similar problem with eclipse kepler.I have followed these steps to resolve it 我遇到了与eclipse kepler类似的问题。我已按照这些步骤来解决它

  • Go to Help in Eclipse and Oper Eclipse Market Place option. 转到Eclipse和Oper Eclipse Market Place选项中的Help。
  • Search for jdk 1.8 for kepler 搜索kepler的jdk 1.8
  • Install the required plugin. 安装所需的插件。
  • Restart the eclipse. 重启日食。

for reference, refer this link http://techno-terminal.blogspot.in/2016/05/jdk-18-compiler-compliance-is-not.html 供参考,请参阅此链接http://techno-terminal.blogspot.in/2016/05/jdk-18-compiler-compliance-is-not.html

Old question, but posting the answer incase it helps someone. 老问题,但发布答案,这有助于某人。 Already build path was configured to use JDK 1.2.81 However, build was failing with the error below: 已经将构建路径配置为使用JDK 1.2.81但是,构建失败并出现以下错误:

 lambda expressions are not supported in -source 1.5
[ERROR]   (use -source 8 or higher to enable lambda expressions)

In the latest Eclipse (Photon), adding the below entry to pom.xml worked. 在最新的Eclipse(Photon)中,将以下条目添加到pom.xml中。

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
  </properties>

This is a old topic but I just wanted to point out that I have searched enough to find that Indigo version can't be updated to SE 1.8 here the link which is given on eclipse website to update the Execution Environment but if you try it will throw error for Indigo. 这是一个古老的话题,但我只是想指出我已经搜索到足以发现Indigo版本无法更新到SE 1.8这里的链接在eclipse网站上给出以更新执行环境但是如果你尝试它将为Indigo抛出错误。

Image //wiki.eclipse.org/File:ExecutionEnvironmentDescriptionInstallation.png this is the link where the Information about execution environment is given. Image //wiki.eclipse.org/File:ExecutionEnvironmentDescriptionInstallation.png这是给出有关执行环境的信息的链接。

https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler This shows the step by step to update Execution environment. https://wiki.eclipse.org/JDT/Eclipse_Java_8_Support_For_Kepler这显示了更新执行环境的一步一步。

I have tried to update Execution environment and I got the same error. 我试图更新执行环境,我得到了同样的错误。

Assuming you have already downloaded Jdk 1.8. 假设您已经下载了Jdk 1.8。 You have to make sure your eclipse version supports Jdk 1.8. 你必须确保你的eclipse版本支持Jdk 1.8。 Click on "Help" tab and then select "Check for Updates". 单击“帮助”选项卡,然后选择“检查更新”。 Try again. 再试一次。

First install the JDK1.8 set to Path Open Eclipse and Oper Eclipse Market Place option. 首先将JDK1.8设置为Path Open Eclipse和Oper Eclipse Market Place选项。 Search for jdk 1.8 for kepler Install the required plugin. 搜索jdk 1.8 for kepler安装所需的插件。 Restart the eclipse. 重启日食。 Change compiler level to 1.8 from preferences. 从首选项将编译器级别更改为1.8。 If still there is an error then click on the file and change the compiler setting explicitly to Jdk 1.8 如果仍有错误,则单击该文件并将编译器设置显式更改为Jdk 1.8

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM