错误:java:错误:源代码版本无效:13 使用带有 IntelliJ 的 JDK12

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

Error:java: error: invalid source release: 13 using JDK12 with IntelliJ

javaintellij-ideajava-12preview-feature

提问by Naman

I am trying to build a project with JDK-12 ea. While trying to execute a sample class:

我正在尝试使用 JDK-12 ea 构建一个项目。在尝试执行示例类时:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int value = scanner.nextInt();
    // After JEP-325
    switch (value) {
        case 1 ->System.out.println("one");
        case 2 ->System.out.println("two");
        default ->System.out.println("many");
    }
} 

The IDE throws the error that reads

IDE 抛出读取错误

Error:java: error: invalid source release: 13

错误:java:错误:源代码无效发布:13

Relevant project configuration screens :

相关项目配置屏幕:

enter image description here

在此处输入图片说明

Module settings

模块设置

enter image description here

在此处输入图片说明

SDKs

SDK

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Compiler settings

编译器设置

enter image description hereAbout IDE:

在此处输入图片说明关于IDE:

IntelliJ IDEA 2018.3.3 (Community Edition)
Build #IC-183.5153.38, built on January 9, 2019
JRE: 1.8.0_152-release-1343-b26 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6
IntelliJ IDEA 2018.3.3 (Community Edition)
Build #IC-183.5153.38, built on January 9, 2019
JRE: 1.8.0_152-release-1343-b26 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Tried switching back the language level to 12, without experimental features, but it ends in

尝试将语言级别切换回 12,没有实验性功能,但它以

enter image description here

在此处输入图片说明

I remember using the experimental features in recent past(not sure of exact IntelliJ version) successfully. Has something changed that I need to configure apart from this as well?

我记得最近成功使用了实验功能(不确定确切的 IntelliJ 版本)。除此之外,我还需要配置一些更改吗?

采纳答案by Jorn Vernee

The right way?

正确的方式?

The earliest version of IntelliJ that supports switch expressions is version 2019.1, which was released on March 27, 2019. You can get it here: https://www.jetbrains.com/idea/download. You also need JDK 12 of course, and set that as your project SDK.

IntelliJ 最早支持 switch 表达式的版本是 2019.1 版本,发布于 2019 年 3 月 27 日,可以在这里获取:https://www.jetbrains.com/idea/download 。当然,您还需要 JDK 12,并将其设置为您的项目 SDK。

Then you can set the language level to:

然后您可以将语言级别设置为:

12 (Preview) - Switch expressions

With that, everything should work.

有了这个,一切都应该有效。

If it doesn't, you might want to check that You've;

如果没有,您可能需要检查您是否已经;

  • set the project language level, as well as the module language level to the "12 (Preview)"
  • set the execution JRE in the run configuration of your application to 12 (or the default, which is the project SDK).
  • set the right bytecode version in Settings -> 'Build, Execution, Deployment'/Compiler/Java Compiler. (Leave this fields empty to match the language level)
  • 将项目语言级别以及模块语言级别设置为“12(预览)”
  • 在你的应用程序的运行配置中将执行 JRE 设置为 12(或默认值,即项目 SDK)。
  • 在 Settings -> 'Build, Execution, Deployment'/Compiler/Java Compiler 中设置正确的字节码版本。(将此字段留空以匹配语言级别)


Manually configure --enable-preview (no intellisense)

手动配置 --enable-preview(无智能感知)

Turning my comment into an answer. You can add the --enable-previewflag to the VM by going to:

把我的评论变成答案。您可以通过以下方式将--enable-preview标志添加到 VM:

Run-> Edit Configurations...

运行->编辑配置...

Then selecting your main class from the tree menu on the left, and pasting --enable-previewin the "VM options" box

然后从左侧的树形菜单中选择您的主类,并粘贴--enable-preview到“VM 选项”框中

enter image description here

在此处输入图片说明

You can do the same for the compiler by going to:

您可以通过以下方式对编译器执行相同操作:

File-> Settings...

文件->设置...

Then in the tree menu under Build, Execution, Deployment-> Compiler-> Java Compileryou can put --enable-previewin the "Additional command line parameters" box:

然后在Build, Execution, Deployment-> Compiler-> Java Compiler下的树形菜单中您可以--enable-preview在“Additional command line parameters”框中输入:

enter image description here

在此处输入图片说明

Note that intellisense still might not work after doing that. I'm still seeing red squiggly lines under the ->s with the error message "unexpected token". But, when I click the run button the class compiles and runs just fine.

请注意,执行此操作后,智能感知可能仍然无法正常工作。我仍然在->s下看到红色波浪线,并显示错误消息“意外令牌”。但是,当我单击运行按钮时,该类会编译并运行得很好。

回答by Olga Klisho

In IDEA v2018.3.2 Previewlanguage level added --enable-previewparameter to command line. In v2018.3.3 there is no 12 Previewlevel, so parameter has to be added manually as you correctly mentioned in your comment. Experimental featuresdoesn't add any compilation parameters.

在 IDEA v2018.3.2预览语言级别中,在命令行中添加了--enable-preview参数。在 v2018.3.3 中没有 12预览级别,因此必须按照您在评论中正确提到的方式手动添加参数。实验性功能不添加任何编译参数。

回答by Mauricio Reis

Just for the record. I was having similar error but with Java 14:

只是为了记录。我有类似的错误,但使用 Java 14:

Error:java: error: invalid source release: 14

What solved the problem for me was to hunt down on every single place of the IDE where there was another Java version being mentioned that wasn't the 14th one.

对我来说解决这个问题的是寻找 IDE 的每一个地方,其中提到了另一个 Java 版本,而不是第 14 个版本。

The places that I had to change were:

我不得不改变的地方是:

File -> Project Structure -> Project Settings

文件 -> 项目结构 -> 项目设置

File -> Project Structure -> Module Settings -> Tab: Sources: Language Level.

文件 -> 项目结构 -> 模块设置 -> 选项卡:来源:语言级别。

File -> Project Structure -> Module Settings -> Tab: Dependencies: Module SDK.

文件 -> 项目结构 -> 模块设置 -> 选项卡:依赖项:模块 SDK。

File -> Settings -> Compiler -> Java Compiler -> Target bytecode version.

文件 -> 设置 -> 编译器 -> Java 编译器 -> 目标字节码版本。