13

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 :

enter image description here

Module settings

enter image description here

SDKs

enter image description here

enter image description here

Compiler settings

enter image description here 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

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?

Naman
  • 27,789
  • 26
  • 218
  • 353
  • 1
    I guess experimental features uses --source 13 ? Have you tried with the --enable-preview flag? – Jorn Vernee Jan 12 '19 at 03:01
  • @JornVernee As stated in the question as well, switching back to setting language level to 12 ( and not experimental) and then trying to build the project reads error on every similar class to use `--enable-preview` for them... I am not aware of such a project level flag setting though. – Naman Jan 12 '19 at 03:05
  • It's a compiler/VM flag, I can get it to work using that. I also have a "12 (preview)" language level available on intellij 2018.3, but that doesn't seem to work with the openjdk nightly I'm using (maybe it will with 12-ea). – Jorn Vernee Jan 12 '19 at 03:18
  • 1
    *"12 (preview)" language level* ... yup that seems to be missing from the IntelliJ for me now. Adding compiler level flag does work. (you can make that as an answer) but that doesn't relate either to the error that IntelliJ stated or even that the actual error was displayed against `java` and not `javac`! – Naman Jan 12 '19 at 03:48
  • 4
    The error in intellij seems to come from `javac`. If I select "12 (preview)" as a language level in the "Messages" I see on the last 2 lines "...Compilation completed with 1 error...", "Error:java: error: release version 12 not supported", and the error is the same as the one I get when using `--release 12` with javac. intellij saying 'java' is talking about the used language I guess... Also, I'm not sure why you don't have "12 (preview)" as a language level, since you seem to have a newer version of intellij than me... – Jorn Vernee Jan 12 '19 at 03:53
  • 1
    @JornVernee True, its(the error) for compiling sources in "java". Yes, I have the recent most IntelliJ. – Naman Jan 12 '19 at 03:59
  • 1
    FWIW I updated to 2018.3.3 and the "12 (preview)" language level has been removed, though I am asked to accept the preview license when selecting "12". I guess they'd rather have users manually specify the `--enable-preview` flag. – Jorn Vernee Jan 12 '19 at 19:52
  • For **[gradle]** (https://stackoverflow.com/a/68859868/410439) – Ravi Parekh Aug 30 '21 at 15:13
  • Duplicate [intellij idea - Error: java: invalid source release 1.9](https://stackoverflow.com/questions/46280859/intellij-idea-error-java-invalid-source-release-1-9) – Ravi Parekh Aug 31 '21 at 08:30
  • @RaviParekh The other question doesn't focus on the `--enable-preview` functionality which is what this question is based on. – Naman Aug 31 '21 at 10:46
  • @Naman, Preview has been used long with intellij but no need to add `--enable-preview` in vm options or compiler, it is just your project need to set on `language level` and intellij will take care. Make sure not to select `x-Experimental` rather select speicific `16 (preview) - Sealed types` – Ravi Parekh Sep 01 '21 at 11:10
  • @RaviParekh I believe if you try working on a JDK preview feature within IntelliJ without IntelliJ explicitly supporting it as an Experimental feature within it, you would still need this. – Naman Sep 01 '21 at 16:09

4 Answers4

18

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

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 Compiler you can put --enable-preview in the "Additional command line parameters" box:

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.

Community
  • 1
  • 1
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
  • How do I keep maven from switching module language level from 12 (Preview) to 12. I have compilerArgs --enable-preview on maven-compiler-plugin. – bourne2program Apr 20 '19 at 17:57
12

Just for the record. I was having similar error but with 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.

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.

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

Mauricio Reis
  • 319
  • 3
  • 9
  • 2
    I specify the version in "File -> Settings -> Compiler -> Java Compiler -> Target bytecode version." that works – CodeFarmer Jun 08 '20 at 06:29
  • I did try that for changing JDK from version 13 to 8, but although that I setted up all that options I had to manually edit file .idea/misc.xml and correct set the JDK version languageLevel attribute: languageLevel="JDK_1_8". Only project-jdk-name attribute was affected by UI settings. – Leandro Arruda Feb 28 '21 at 15:28
  • I was having the same issue with jdk 16. Changing Module SDK did it for me. – Piyush Shrivastava Sep 09 '21 at 05:14
3

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.

Olga Klisho
  • 1,206
  • 5
  • 7
0

Assuming you are facing the issue for an SpringBoot project with dependencies mentioned in pom.xml (Maven dependencies). Do the below.

  1. Check the jdk version of your machine using cmd or terminal with command : java -version.
  2. In your pom.xml, update the java version <java.version><MACHINE'S JDK VERSION></java.version>

Save the changes, and you're good to go.