Eclipse 2018-09 不会编译 Java 11 源代码;认为它低于 1.7

我在 Windows 10 上运行 Eclipse 2018-09 (4.9.0)。我使用的是 Open JDK 11 GA。我有一个指定为使用 Java 8 源代码的 Maven 项目。


<properties>

  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  <maven.compiler.source>1.8</maven.compiler.source>

  <maven.compiler.target>1.8</maven.compiler.target>

</properties>

在命令行上使用 Maven 3.5.3 编译得很好。使用 Eclipse Eclipse 2018-09 (4.9.0) 也可以很好地编译。


我将编译 Java 版本更改为 Java 11:


<properties>

  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  <maven.compiler.source>11</maven.compiler.source>

  <maven.compiler.target>11</maven.compiler.target>

</properties>

使用 Maven 3.5.3 在命令行上仍然可以很好地构建。但是在 Eclipse 2018-09 中,我到处都是错误:


'<>' operator is not allowed for source level below 1.7

Constructor references are allowed only at source level 1.8 or above

Default methods are allowed only at source level 1.8 or above

你明白了。


我已经习惯Alt+F5在 Eclipse 中更新我的 Maven 项目(和子项目)。我已经完成了彻底的清理和重建。


因为在命令行上使用 Maven 编译得很好,所以这一定是 Eclipse 问题,不是吗?当然,Eclipse 尚不支持所有新的 Java 11 功能,但此代码没有 Java 11 特定功能。怎么了?


MMMHUHU
浏览 310回答 2
2回答

慕标5832272

听起来 Eclipse 没有从 pom.xml 文件中获取版本。我刚刚使用最新的Java 11 Support for Eclipse 2018-09插件测试了您的 pom 配置并验证了它是否有效,方法是提供问题中的编译器源和目标属性或本答案中描述的新版本属性。在 Eclipse 首选项中正确设置 JDK 11 很重要。在“已安装的 JRE”首选项页面上添加 JDK 11,然后将其与“执行环境”首选项页面上的 JavaSE-11 进行匹配。否则更新 Maven 项目将导致使用默认的 JDK,这可能是您遇到的问题。

牛魔王的故事

更新:Java 11从 4.10 版(2018 年 12 月 19 日发布)开始完全集成在Eclipse 中,因此不再需要安装此插件。出于某种原因,您需要安装一个额外的 Eclipse 插件“ Java 11 Support for Eclipse 2018-09 (4.9) ”(即使在 Eclipse Photon 4.9 中)看来该插件在 Eclipse Marketplace 中不再可用。我有人找到了它的安装网址,请在此处添加:适用于 Eclipse - STS 4.0.1(基于 Eclipse 4.9)、Maven 3.6.0,带有这个 Maven Compiler Plugin Configuration&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-compiler-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.8.0</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <source>11</source>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <target>11</target>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </plugin>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java