IntelliJ indicating that enhanced switch block doesn't work despite using Java 14

Viewed 1591

I am trying to use the enhanced switch block that was standardized in Java 14. The old version is this:

switch (expression) {
    case OPTION1:
        foo();
        break;
    case OPTION2:
        bar();
        break;
}

however since I am using Java 14, I should be able to use this:

switch (expression) {
    case OPTION1 -> foo();
    case OPTION2 -> bar();
}

Here is a MRE:

public class Main {
    public static void main(String[] args) {
        String demo = Math.random() > 0.5 ? "Hello": "World";
        switch (demo) {
            case "Hello" -> System.out.println("The string contained Hello");
            case "World" -> System.out.println("The string contained World");
        }
    }
}

The code in my IDE

When I write that code, IntelliJ gives me the following warning:

Enhanced 'switch' blocks are not supported at language level '14'

Enhanced 'switch' blocks are not supported at language level '14'

However, that warning, despite being highlighted in red to indicate a compile time error, is incorrect. The code compiles correctly and the switch block works as intended because I am using Java 14. Why am I getting this warning?

Edit: Project structure per request

Project structure

Edit: My iml file

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library">
      <library>
        <CLASSES>
          <root url="jar://$USER_HOME$/Downloads/MathParser.org-mXparser-v.4.4.0/MathParser.org-mXparser-v.4.4.0/bin/jdk13/MathParser.org-mXparser-v.4.4.0-jdk13.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>
    </orderEntry>
  </component>
</module>
2 Answers

First use intellij 2020

Normally the reason for this is the sdk, jdk and language level are not aligned,

Check in the project structure project tab that Project and Modules are consistent see below (ok I am using language level 11)

enter image description here enter image description here

Project showing openjdk-14 enter image description here

ide inserted java14.iml file contents

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_14" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/com" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
  </component>
</module>

I have tried this and it is working just fine I think you might forget to add the package name

package com.company;

public class Main
{
    public static void main(String[] args) {
        
        String demo = Math.random() > 0.5 ? "Hello": "World";
        switch (demo)
        {
            case "Hello" -> System.out.println("The string contained Hello");
            case "World" -> System.out.println("The string contained World");
        }
    }
}

This is a screenshot for my project structure is try to compare it with yours

enter image description here

Related