Intellij - 无法使用较新的Java 8类 - 错误:"使用API​​记录为@since 1.6+ .."

Ner*_*rve 163 intellij-idea java-8

我正在尝试java.lang.function.Function在我的java 8代码库中使用a ,但我在Intellij中不断收到以下错误.

使用API​​记录为@since 1.6+此检查查找在其文档中具有@since标记的方法的所有用法.当在较新的SDK版本下执行开发作为生产的目标平台时,这可能很有用

我似乎有正确的项目和编译器设置

项目设置:(文件 - >项目结构)

Project Settings -> Project -> Project SDK = Java 1.8
Project Settings -> Project -> Project Language Level = 8 - Lambdas, Type Annotations etc
Run Code Online (Sandbox Code Playgroud)

编译器设置:(文件 - >设置)

Build, Execution, Deployment -> Compiler -> Java Compiler -> Project Bytecode Version : 1.8
Build, Execution, Deployment -> Compiler -> Java Compiler -> Per module Bytecode Version -> Target Bytecode Version : 1.8
Run Code Online (Sandbox Code Playgroud)

问题是什么?

Ner*_*rve 355

根据Bastien Jansen的评论编辑了答案.

似乎有另一个项目设置会影响编译器级别.这个问题的一个微妙的指示是当你的编译器开始抱怨你编译代码时源和目标java版本与你指定的版本不同

Warning:java: source value 1.5 is obsolete and will be removed in a future release
Warning:java: target value 1.5 is obsolete and will be removed in a future release
Warning:java: To suppress warnings about obsolete options, use -Xlint:-options.
Run Code Online (Sandbox Code Playgroud)

要摆脱这个,你需要打开

File -> Project Structure -> Project Settings -> Modules -> "Your Module Name" -> Sources -> Language Level

并将其更改为所需级别,即1.8或项目默认语言级别

  • `Project Settings`在`File - > Project Structure`中找到 (7认同)
  • 在项目设置中,您应该将每个模块配置为使用"Project default"语言级别(在"Sources"选项卡中). (5认同)
  • 为什么需要在很多地方设置这个以外的东西.谢谢. (4认同)

Aja*_*mar 46

如果您正在使用maven,请在配置pom.xml文件中添加以下行,然后从maven重新导入或构建它.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

否则从下面的路径中选择java编译器和语言级别.

文件>项目结构>项目设置>模块>您的模块名称>来源>语言级别>选择您需要的那个.

在此输入图像描述

从这里更改语言级别: -

在此输入图像描述


Bor*_*hov 31

实际上,如果您正在使用Maven并且pom.xml正确配置了项目属性

<project xmlns="...> 
....
<properties>
         <maven.compiler.source>1.8</maven.compiler.source>
         <maven.compiler.target>1.8</maven.compiler.target>
</properties>
...
</project
Run Code Online (Sandbox Code Playgroud)

您可以将Maven参数重新导入intellij-idea项目 - 右键单击​​项目根目录,然后单击Maven -> Reimport底部.

图片显示Maven是项目右键菜单中最后一项的第二名


Hea*_*ren 19

我刚修好如下:

右键单击项目 - >打开模块设置 - >模块 - >源 - > 8或更高

在此输入图像描述

然后

在此输入图像描述

如果仍然遇到错误并使用maven,则必须在以下位置添加构建配置pom.xml:

<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>
Run Code Online (Sandbox Code Playgroud)