这些天我从事Java14特性。我可以编写一个关于 switch case 的代码片段,并创建了一个动态 Web 项目来使用这个命令 --enable-preview 来运行任何基于 Java14 的代码,但我有一个问题。我尝试了很多方法来解决它,但没有任何改变。我该如何解决?
这是我在控制台上出现的错误。
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
这是我的代码片段
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");
}
}
这是我的 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>