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

浪尽此生 提交于 2019-12-07 06:58:24

问题


I am trying to build a project with 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

Error:java: error: invalid source release: 13

Relevant project configuration screens :

Module settings

SDKs

Compiler settings

About 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

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

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?


回答1:


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.

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)

Manually configure --enable-preview (no intellisense)

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

Run -> Edit Configurations...

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

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:

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.




回答2:


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



来源:https://stackoverflow.com/questions/54156370/errorjava-error-invalid-source-release-13-using-jdk12-with-intellij

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!