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

相关推荐
何中应1 天前
Nexus如何设置端口号
java·服务器·maven·nexus
熟悉的新风景1 天前
maven常用依赖
java·maven
wyhwust2 天前
如何让maven帮我们去下载合适的包
java·maven
jiayong232 天前
Maven clean 报错与 Maven Profile 机制总结
java·maven
我命由我123452 天前
Android 开发,FragmentPagerAdapter 的 isViewFromObject 方法问题
android·java-ee·kotlin·android studio·android jetpack·android-studio·android runtime
我登哥MVP2 天前
Spring Boot 从“会用”到“精通”:请求映射原理
java·spring boot·后端·spring·servlet·maven·intellij-idea
我登哥MVP2 天前
Spring Boot 从“会用”到“精通”:Converter 原理
java·spring boot·servlet·maven·mybatis·converter
我登哥MVP3 天前
Spring Boo从“会用”到“精通”:Spring Boot 入门
java·spring boot·后端·spring·maven·intellij-idea·mybatis
我登哥MVP3 天前
Spring Boot 从“会用”到“精通”:自动装配原理
java·spring boot·后端·spring·tomcat·maven·intellij-idea
Yvonne爱编码3 天前
JAVA EE初阶---DAY 1 计算机是如何工作的
java·java-ee