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

执行mvn spring-boot:run遇invalid target release:17编译错误求助

问题解决:Maven编译报错invalid target release:17(本地Java版本为1.8)

问题场景

学习Spring Boot教程时,执行mvn spring-boot:run命令触发编译失败,错误提示Fatal error compiling: invalid target release: 17。本地已确认Java和Maven均使用1.8版本,但问题依然存在。

错误日志

$ mvn spring-boot:run
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.example:myproject >------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:3.0.0-SNAPSHOT:run (default-cli) > test-compile @ myproject >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /Users/dnatochy/development/springboot/testapp1/src/main/resources
[INFO] skip non existing resourceDirectory /Users/dnatochy/development/springboot/testapp1/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ myproject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/dnatochy/development/springboot/testapp1/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.078 s
[INFO] Finished at: 2022-07-26T21:27:48-07:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project myproject: Fatal error compiling: invalid target release: 17 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

本地环境信息

$ java -version
java version "1.8.0_333"
Java(TM) SE Runtime Environment (build 1.8.0_333-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.333-b02, mixed mode)

$ javac -version
javac 1.8.0_333

$ mvn -v
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /Users/myuser/tools/apache-maven-3.8.6
Java version: 1.8.0_333, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_333.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "12.4", arch: "x86_64", family: "mac"

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_333.jdk/Contents/Home

解决方案

1. 修正Spring Boot版本兼容性

从错误日志可见使用的是spring-boot-maven-plugin:3.0.0-SNAPSHOT,Spring Boot 3.x系列最低要求Java 17,与你本地Java 8版本不兼容。两种解决路径:

  • 升级本地Java环境到17及以上版本;
  • 降级Spring Boot版本到2.x系列(如2.7.x),该系列完全兼容Java 8。

2. 显式指定Maven编译版本

若坚持使用Java 8,需在项目pom.xml中强制配置编译版本:

<properties>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

同时确保父依赖为2.x版本:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.14</version> <!-- 2.x兼容Java8的稳定版本 -->
    <relativePath/>
</parent>

3. 排查Maven全局配置

检查Maven安装目录下的conf/settings.xml或用户目录下的.m2/settings.xml,确认是否有全局profile强制指定Java 17版本,如有则修改为1.8。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 04:15:44