此语言级别不支持 Java- Diamond 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44005097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:52:42  来源:igfitidea点击:

Java- Diamond types are not supported at this language level

javamavenintellij-ideaversiondiamond-operator

提问by Noble-Surfer

I have just started working on a Java project, and have downloaded the source code from GitHub, using IntelliJ- I have never used IntelliJ before, but am told that it is a much better IDE to use than Eclipse (which is what I was using when I last did any Java development- about four years ago).

我刚刚开始处理一个 Java 项目,并使用 IntelliJ 从 GitHub 下载了源代码 - 我以前从未使用过 IntelliJ,但被告知它是一个比 Eclipse 更好的 IDE(这是我使用的当我上次进行任何 Java 开发时 - 大约四年前)。

When I try to build the source locally on my computer, having pulled the latest working version from GitHub, I get a compile error on several different lines of code- the error says:

当我尝试在我的计算机上本地构建源代码时,从 GitHub 中提取了最新的工作版本,我在几行不同的代码中遇到编译错误 - 错误说:

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

错误:(27, 34) java: -source 1.5 不支持菱形运算符(使用 -source 7 或更高版本启用菱形运算符)

and the lines where these compile errors appear, are lines like:

出现这些编译错误的行是这样的:

return new ArrayList<>(0);

If I select the line, and do Alt + Enteron the error, it shows a message stating that I can

如果我选择该行,并Alt + Enter针对错误执行操作,则会显示一条消息,说明我可以

"Set language level to 7- Diamonds, ARM, Multi-cache, etc"

“将语言级别设置为 7-Diamonds、ARM、Multi-cache 等”

However, if I select this option, nothing happens...

但是,如果我选择此选项,则没有任何反应...

In the pom.xmlfile, there is the following xml:

pom.xml文件中,有以下xml:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>${maven-source-plugin.version}</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

But when I looked this error up, I came across the answer at: Diamond type are not supported at this language level, which indicated that I should be using maven1.7 or higher- and it appears that the project is already using version 1.8, so I don't understand why I'm getting this compile error...

但是当我查看这个错误时,我发现答案是:Diamond type are not supported at this language level,这表明我应该使用 maven1.7 或更高版本 - 并且该项目似乎已经在使用版本 1.8,所以我不明白为什么我会收到这个编译错误......

Anyone have any suggestions?

有人有什么建议吗?

回答by user8936069

Add the following code into your pom.xmlfile.

将以下代码添加到您的pom.xml文件中。

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

回答by DrLime2k10

Under File->Project Structure(Ctrl+ Alt+ Shift+ s) there's a section called Project Settings, and Project

File->Project Structure( Ctrl+ Alt+ Shift+ s) 下有一个名为 的部分Project Settings,并且Project

Here you can set the project language level. enter image description here

您可以在此处设置项目语言级别。 在此处输入图片说明