0

I'm engaged in Java14 features in these days. I could write a code snippet about switch case and I created a dynamic web project to use this command --enable-preview to run any code based on Java14 but I have a problem about it. I tried many things to solve it out but nothing changed. How can I fix it out?

Here is my error appeared on the console.

Error: LinkageError occurred while loading main class switchcase1.SwitchMain
    java.lang.UnsupportedClassVersionError: switchcase1/SwitchMain (class file version 57.65535) was compiled with preview features that are unsupported. This version of the Java Runtime only recognizes preview features for class file version 58.65535

Here is my code snippet

private static void getDayInJava14(int day) {
        // TODO Auto-generated method stub

        switch (day) {
        case 1-> {
            System.out.println("Monday");
        }
        case 2-> {
            System.out.println("Tuesday");
        }
        case 3-> {
            System.out.println("Wednesday");
        }
        case 4 -> System.out.println("Thursday");
        case 5 -> System.out.println("Friday");
        case 6 -> System.out.println("Saturday");
        case 7 -> System.out.println("Sunday");
        default -> System.out.println("Invalid day"); 

        }

    }

Here is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>002_Java_Switch</groupId>
  <artifactId>002_Java_Switch</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>14</java.version>
   </properties>



  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <release>${java.version}</release>
          <compilerArgs>
             --enable-preview
          </compilerArgs>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <argLine>--enable-preview</argLine>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
S.N
  • 2,157
  • 3
  • 29
  • 78
  • @user85421 Where should I change in pom.xml – S.N Apr 02 '20 at 22:49
  • 2
    You need to have Maven use Java 14 when building your project. What does `JAVA_HOME` point to? – Slaw Apr 02 '20 at 23:07
  • 2
    Preview features exist so that we can experiment with them, and provide feedback on the feature to the language designers before they become permanent parts of the language. They come with strict guardrails (need to use `--enable-preview` at compile and run time, cannot run classes using preview features generated by JDK N on JDK N+1) to prevent people from unexpectedly depending on something that may change or go away (say, by someone compiling a library with `--enable-preview` and putting it in Maven Central, and the poor consumer has no idea they are depending on something unstable.) – Brian Goetz Apr 03 '20 at 15:57

0 Answers0