You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven构建Spring Boot Jar失败:Java版本不匹配问题求助

Spring Boot打包报错:release version 21 not supported 解决方法

问题场景

执行Jar包构建命令时失败:

./mvnw clean package

核心报错信息:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project JayathilakaPosBackedWithPrinter: Fatal error compiling: error: release version 21 not supported

pom.xml中已配置java.version为21,且系统Java版本与配置一致,但仍出现上述报错。

解决方法

1. 检查Maven Wrapper使用的JDK版本

Maven Wrapper可能未使用系统默认JDK,执行命令查看当前wrapper绑定的JDK:

./mvnw -v

若显示版本不是21,可通过两种方式修正:

  • 修改项目根目录下.mvn/wrapper/maven-wrapper.properties文件,指定JDK 21的路径;
  • 确保系统环境变量JAVA_HOME指向完整的JDK 21安装目录。

2. 升级maven-compiler-plugin版本

当前使用的3.11.0版本对JDK 21支持不完善,升级到3.12.0及以上版本:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.12.1</version>
    <configuration>
        <source>21</source>
        <target>21</target>
        <compilerArgs>
            <arg>--enable-preview</arg>
        </compilerArgs>
    </configuration>
</plugin>

3. 利用Spring Boot父依赖简化配置

Spring Boot父依赖已集成maven-compiler-plugin的默认配置,无需单独指定版本和source/target,仅保留必要的preview参数即可:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <compilerArgs>
                    <arg>--enable-preview</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

4. 验证JDK 21安装完整性

执行命令确认javac版本是否为21:

javac -version

若版本不符,说明仅安装了JRE而非完整JDK,需重新安装完整的JDK 21。

内容的提问来源于stack exchange,提问作者Rashmika Abeyrathna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 14:58:08