IntelliJ IDEA 13使用 Java 1.5,尽管设置为1.7

尽管在所有项目设置中(包括在 File -> Project Structure -> Project :: Project SDK中)都指定了 JDK 1.7,但是在尝试编译一些使用菱形运算符的简单 Java 7代码时,IntelliJ 13会产生以下错误:

java: diamond operator is not supported in -source 1.5
(use -source 7 or higher to enable diamond operator)

在配置中是否还有其他地方应该启用预期的 -source 7选项?

110392 次浏览

请检查您的项目/模块语言级别(项目结构 | 项目; 项目结构 | 模块 | 模块名称 | 源)。您可能还需要查看 Settings | Compiler | Java Compiler | Per-module 字节码版本。

还要这样设置:

文件-> 项目结构-> 模块: : 来源(旁边的路径和依赖) ,并有一个“语言级别”选项,也需要正确设置。

首先,您需要更改 File > SettingsCompiler > Java Compiler下的“项目字节码版本”

第二,全面重建。

如果这些都没有帮助(在我的例子中) ,您可以在 pom.xml 中设置它,如下所示:

<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

正如这个酷哥提到的: Https://stackoverflow.com/a/25888116/1643465

或者,您也可以通过将其添加到 pom.xml 中,使用适当的 java 版本应用 maven 编译器插件:

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

[ For IntelliJ IDEA 2016.2]

我想用一个最新的截图扩展 Peter Gromov 的回答的一部分。 特别是这部分:

您可能还需要查看 Settings | Compiler | Java Compiler | Per-module 字节码版本。

我相信(至少在2016.2) : 在 git中检查不同的提交会将这些重置为1.5。

Per-module bytecode version

我有同样的问题,但处境不同。我可以在命令行(mvn clean install)中使用 maven 编译,但是在 Intellij,尽管我在 pom.xml 中使用 java 1.8设置了 maven 编译器插件,但是我总是会得到 "java: diamond operator is not supported in -source 1.5"编译错误。

事实证明,我在我的专家的 setings.xml 中有远程存储库设置,这是项目所依赖的,但 Intellij 使用他自己的专家,这与我的本地专家没有相同的设置。

因此,我的解决方案是更改 Intellij 的 maven 设置(Settings -> Build, execution, Deployment -> Maven -> Maven home directory)以使用本地 maven。

我尝试对 Intellij IDEA 做出以下改变:

1.

File >> Settings >> Build, Execution, Deployment >> Compiler >> Java Compiler >> project bytecode version: 1.8 >> Per-module bytecode version: 1.8

2.

File >> Project Structure >> Project Settings >> Project >> SDK : 1.8, Project Language : 8 - Lambdas
File >> Project Structure >> Project Settings >> Modules >> abc : Language level: 8 - Lambdas

但是没有任何效果,我一保存它,它就把版本恢复到 java 1.5。

然而,向 root (项目级别) pom.xml 添加以下代码使我解决了上述问题: (这两个选项对我都有效)

选择1:

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

选择2:

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

在命令行(Unix 终端)中,转到项目根文件夹,并执行以下操作

find . -type f -name '*.iml' -exec sed -i '' s/JDK_1_5/JDK_1_8/g {} +

这将把所有 project. iml 文件中的语言级属性从 java 1.5更改为 java 1.8。

文件-> 项目结构-> 项目设置-> 项目-> 项目语言级别

文件-> 项目结构-> 项目设置-> 模块-> 语言级别

使用下拉更改级别

在 IntelliJ 2017中,以下属性为我工作

  <properties>
<java.version>1.8</java.version>
</properties>

另一个可能导致这种情况的原因是 <parent>项目的 version不正确。

在我的案例中,它指向一个不存在的项目,出于某种原因,IntelliJ 将版本设置为1.5,后来我修复了它,它仍然将目标代码版本解释为5(尽管设置为11)。

在 IntelliJ Community Edition 2019.02中,更改以下设置对我很有用

  1. 使用下拉列表更新文件-> 项目结构-> 项目设置-> 项目-> 项目语言级到 Java11(更新到您希望在项目中使用的 Java 版本)。

  2. 更新文件-> 项目结构-> 项目设置-> 模块-> 语言级别

  3. 更新文件-> 设置-> 构建,执行,部署-> 编译器-> Java 编译器-> 项目字节码版本到 Java 11。

  4. 更新 File-> Settings-> Build,Execution,Deployment-> Compiler-> Java Compiler-> Per module Byte Code Version 下所有条目的目标版本。

我设法通过更改新项目的设置来解决这个问题:

  1. 文件-> 新项目设置-> 新项目设置-> Java 编译器-> < strong > 设置版本

  2. 文件-> 新项目设置-> 新项目的结构-> Project-> < strong > Set Project SDK + Set language level

  3. 删除项目

  4. 导入项目

IntelliJ 中似乎有一些影响源代码(字节码)版本的地方:

• IntelliJ > Preferences >Build Exce Deploy > Java Compiler
◦ Module > Target Bytecode Version
• File > Project Structure
◦ Project > Language Level & SDK
◦ Module > Language Level
• Maven Properties
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>