简体   繁体   English

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

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

I am trying to build a project with JDK-12 ea.我正在尝试使用 JDK-12 ea 构建一个项目。 While trying to execute a sample class:在尝试执行示例类时:

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 :相关项目配置屏幕:

在此处输入图片说明

Module settings模块设置

在此处输入图片说明

SDKs SDK

在此处输入图片说明

在此处输入图片说明

Compiler settings编译器设置

在此处输入图片说明 About 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 sro macOS 10.13.6

Tried switching back the language level to 12, without experimental features, but it ends in尝试将语言级别切换回 12,没有实验性功能,但它以

在此处输入图片说明

I remember using the experimental features in recent past(not sure of exact IntelliJ version) successfully.我记得最近成功使用了实验功能(不确定确切的 IntelliJ 版本)。 Has something changed that I need to configure apart from this as well?除此之外,我还需要配置一些更改吗?

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 . IntelliJ 最早支持 switch 表达式的版本是 2019.1 版本,发布于 2019 年 3 月 27 日,可以在这里获取: https ://www.jetbrains.com/idea/download。 You also need JDK 12 of course, and set that as your project SDK.当然,您还需要 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)"将项目语言级别以及模块语言级别设置为“12(预览)”
  • set the execution JRE in the run configuration of your application to 12 (or the default, which is the project SDK).在你的应用程序的运行配置中将执行 JRE 设置为 12(或默认值,即项目 SDK)。
  • set the right bytecode version in Settings -> 'Build, Execution, Deployment'/Compiler/Java Compiler.在 Settings -> 'Build, Execution, Deployment'/Compiler/Java Compiler 中设置正确的字节码版本。 (Leave this fields empty to match the language level) (将此字段留空以匹配语言级别)

Manually configure --enable-preview (no intellisense)手动配置 --enable-preview(无智能感知)

Turning my comment into an answer.把我的评论变成答案。 You can add the --enable-preview flag 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-preview in the "VM options" box然后从左侧的树形菜单中选择您的主类,并在“VM 选项”框中粘贴--enable-preview

在此处输入图片说明

You can do the same for the compiler by going to:您可以通过以下方式对编译器执行相同操作:

File -> Settings...文件->设置...

Then in the tree menu under Build, Execution, Deployment -> Compiler -> Java Compiler you can put --enable-preview in the "Additional command line parameters" box:然后在Build, Execution, Deployment -> Compiler -> Java Compiler下的树形菜单中您可以将--enable-preview放在“附加命令行参数”框中:

在此处输入图片说明

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".我仍然在-> s 下看到红色波浪线,并显示错误消息“意外令牌”。 But, when I click the run button the class compiles and runs just fine.但是,当我单击运行按钮时,该类会编译并运行得很好。

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 编译器 -> 目标字节码版本。

In IDEA v2018.3.2 Preview language level added --enable-preview parameter to command line.在 IDEA v2018.3.2预览语言级别中,在命令行中添加了--enable-preview参数。 In v2018.3.3 there is no 12 Preview level, so parameter has to be added manually as you correctly mentioned in your comment.在 v2018.3.3 中没有 12预览级别,因此必须像您在评论中正确提到的那样手动添加参数。 Experimental features doesn't add any compilation parameters.实验性功能不添加任何编译参数。

Assuming you are facing the issue for an SpringBoot project with dependencies mentioned in pom.xml (Maven dependencies).假设您面临 SpringBoot 项目的问题,该项目具有 pom.xml 中提到的依赖项(Maven 依赖项)。 Do the below.执行以下操作。

  1. Check the jdk version of your machine using cmd or terminal with command : java -version.使用 cmd 或带有命令的终端检查您机器的 jdk 版本:java -version。
  2. In your pom.xml, update the java version <java.version><MACHINE'S JDK VERSION></java.version>在您的 pom.xml 中,更新 java 版本 <java.version><MACHINE'S JDK VERSION></java.version>

Save the changes, and you're good to go.保存更改,您就可以开始了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM