异常
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project sb3: Compilation failure
[ERROR] --enable-preview 一起使用时无效
[ERROR] (仅发行版 21 支持预览语言功能)
解决:
设置启idea的启动类设置如下图:
 1.
 
 2.
 
4.在springboot的maven中pom.xml中添加,不然不能打jar包
只贴主要的,其他该咋写就咋写
java.version=21
主要的:
 <release>21</release>
 <compilerArgs>
  <arg>--enable-preview</arg>
  </compilerArgs>
我的可能和你的配置不一样,主要的添加--enable-preview即可
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <!-- 解决maven命令编译报错,因为rt.jar 和jce.jar在jre的lib下面,不在jdk的lib下面,
                        导致maven找不到(java7以后会出现这个问题)-->
                        <bootclasspath>${java.home}\lib\rt.jar;${java.home}\lib\jce.jar</bootclasspath>
                    </compilerArguments>
                    <release>21</release>
                    <compilerArgs>
                        <arg>--enable-preview</arg>
                    </compilerArgs>
                    
                </configuration>
            </plugin>
如图:
 
启动jar命令也得设置–enable-preview
使用了JDK预览特性进行了编译,但是在运行时没有启用预览特性支持。
解决方法是在启动应用时加上JVM参数–enable-preview:
启动jar包命令设置:
java --enable-preview -jar your-app.jar
在IDE里运行时配置这个JVM参数。
这会启用预览特性支持,从而避免这个UnsupportedClassVersionError。
注意,这只是在开发调试阶段需要的配置。在生产环境部署时,需要去掉–enable-preview,避免使用了预览特性的代码在正式环境运行出错。
总结一下:
开发环境编译和运行需要启用–enable-preview
生产环境部署需要去掉该参数
尽量避免在生产代码中使用预览特性
这样可以很好地区分开发和生产环境对JDK预览特性的处理。




![[LeetCode]-225. 用队列实现栈](https://img-blog.csdnimg.cn/de34221d0eb844148091245e07e4b19b.png)













