使用jdk21预览版 --enable-preview

异常

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的启动类设置如下图:

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预览特性的处理。

相关推荐
※DX3906※18 小时前
SpringBoot之旅4: MyBatis 操作数据库(进阶) 动态SQL+MyBatis-Plus实战,从入门到熟练,再也不踩绑定异常、SQL拼接坑
java·数据库·spring boot·spring·java-ee·maven·mybatis
Meepo_haha20 小时前
Maven Spring框架依赖包
java·spring·maven
想进大厂的小徐2 天前
maven的子模块和子pom的区别
java·maven
Samson Bruce2 天前
【Maven Spring Nacos之profile】
maven
弹简特2 天前
【JavaEE】MyBatis-Plus 条件构造器速查表
java-ee·mybatis
Skilce2 天前
ZrLog 高可用部署
运维·服务器·数据库·阿里云·maven
二进制person2 天前
JavaEE进阶 --Spring Framework、Spring Boot和Spring Web MVC(2)
spring boot·spring·java-ee
competes3 天前
深圳程序员职业生涯
java·大数据·开发语言·人工智能·java-ee
Boop_wu3 天前
[Java EE 进阶] Spring Boot 日志全面解析 : 配置与实战
junit·java-ee·单元测试