Java 无法在 IntelliJ 中使用 jdk 11 进行编译,找不到符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52510671/
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-08-11 00:29:23  来源:igfitidea点击:

Can't compile with jdk 11 in IntelliJ, cannot find symbol

javaintellij-ideajava-11

提问by starsuper

I set the JDK 11, it compiles until I use the new method of Java 11 isBlank()of a String when I use that method this error appears when compiling, I tried cleaning the JDK installations, cleaning caches from IntelliJ, rebuilding but nothing helps. The error is:

我设置了 JDK 11,它会一直编译,直到我使用isBlank()字符串的 Java 11 的新方法为止,当我使用该方法时,编译时出现此错误,我尝试清理 JDK 安装,从 IntelliJ 清理缓存,重建但没有任何帮助。错误是:

enter image description here

在此处输入图片说明

回答by Pim Hazebroek

You can use JDK 11 to compile but if you compile against an older version of java, it cannot find that method.

您可以使用 JDK 11 进行编译,但如果您针对旧版本的 Java 进行编译,则无法找到该方法。

Go to File > Project Structure -> Projectand check the project language level as shown by the arrow in the picture below:

转到File > Project Structure -> Project并检查项目语言级别,如下图箭头所示:

Not working with project language level 10: Wrong project language level

不使用项目语言级别 10: 错误的项目语言级别

Working fine with project language level 11: Working project language level

使用项目语言级别 11 可以正常工作: 工作项目语言水平

Note that I have the following Java Compiler settings (default settings for a fresh new project using IntelliJ 2018.3): The project bytecode version is same as language level! Java compiler settings

请注意,我有以下 Java 编译器设置(使用 IntelliJ 2018.3 的全新项目的默认设置):项目字节码版本与语言级别相同! Java编译器设置

回答by caco3

Try setting compiler target bytecode version to 11.

尝试将编译器目标字节码版本设置为 11。

Go to Settings- Build, Execution, Deployment- Compiler- Java compilerand set target bytecode versionof your moduleto 11

Settings- Build, Execution, Deployment- Compiler-Java compiler并设置目标字节代码的版本你的模块11

回答by Rostislav Krasny

You should check following things:

您应该检查以下事项:

  1. You've JDK11 defined as one of the SDKs: enter image description here
  2. Your project's default SDK is JDK11 and your projects default language level is 11: enter image description here
  3. Your module language level is also 11. enter image description here
  1. 您已将 JDK11 定义为 SDK 之一: 在此处输入图片说明
  2. 您项目的默认 SDK 是 JDK11,您的项目默认语言级别是 11: 在此处输入图片说明
  3. 您的模块语言级别也是 11。 在此处输入图片说明

And if you use Maven, check that your compiler plugin "source" and "target" properties in your pom.xmlare 11. Your module language level configuration is imported from that pom.xmlconfiguration in such a case

如果您使用 Maven,请检查您的编译器插件“源”和“目标”属性pom.xml是否为 11。pom.xml在这种情况下,您的模块语言级别配置是从该配置导入的

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

or

或者

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
            <source>11</source>
            <target>11</target>
        </configuration>
     </plugin>

In case your pom.xmlhad wrong source or target level you may need to do Right-click | Maven | Reimport after changing that pom.xml. In case you use Gradle you should check that you have a similar configuration.

如果您pom.xml有错误的源或目标级别,您可能需要执行右键单击 | 马文 | 更改后重新导入pom.xml。如果您使用 Gradle,您应该检查您是否有类似的配置。

  1. Your module SDK is Project JDK11 or just JDK11 enter image description hereenter image description here
  1. 您的模块 SDK 是 Project JDK11 或只是 JDK11 在此处输入图片说明在此处输入图片说明

Tested with Maven 3.5.4 in IntelliJ IDEA 2018.2.4

在 IntelliJ IDEA 2018.2.4 中使用 Maven 3.5.4 进行测试

回答by Omar Faroque Anik

For my case, it was not even showing 11 in Project language level. So I had to choose X like below screenshot

就我而言,它甚至没有在项目语言级别显示 11。所以我不得不像下面的截图一样选择 X

enter image description here

在此处输入图片说明

回答by batchmode

I had a similar problem. Got symbol not foundfor a Java 11 feature. Though everything was set to 11 already.

我有一个类似的问题。有没有发现标志为Java 11功能。虽然一切都已经设置为 11。

The solution was that in my Idea settings the Gradle JVM was set to JDK 8. (because I added JDK 11 later)

解决方案是在我的 Idea 设置中将 Gradle JVM 设置为 JDK 8。(因为我后来添加了 JDK 11)

As sourceCompatibiltyand targetCompatibilitywhere not set in my build.gradle the gradle daemon (started from Idea) was running and compiling with JDK 8. These properties where dynamically set to 8 (or 1.8).

作为sourceCompatibiltytargetCompatibility凡在我的build.gradle没有设置gradle这个守护进程(从理念开始)正在运行,并与JDK 8,其中动态设置为8(或1.8)这些属性进行编译。

Telling Idea to set JVM for Gradle to 11 was my solution:

告诉 Idea 将 Gradle 的 JVM 设置为 11 是我的解决方案:

enter image description here

在此处输入图片说明