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

Spring Boot项目执行mvn命令编译报错:For input string: "1.8"

解决Spring Boot项目Maven编译错误:For input string: "1.8"

问题场景

新建Spring Boot项目,在IntelliJ中可正常运行,但执行mvn spring-boot:run命令时出现编译错误。

错误信息

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:testCompile (default-testCompile) on project example: **Execution default-testCompile of goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:testCompile failed: For input string: "1.8" -> [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/PluginExecutionException

项目代码

package com.abc.example;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ExampleApplication implements CommandLineRunner
{

    public static void main(String[] args) {
        SpringApplication.run(ExampleApplication .class, args);
        System.out.println("Hello 1");
    }

    @Override
    public void run(String... args) throws Exception
    {
        System.out.println("Hello 2");
    }
}

pom.xml片段

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/>
    </parent>
    <groupId>com.abc</groupId>
    <artifactId>example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>example</name>
    <description>example</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <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>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Java环境配置

java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
echo %JAVA_HOME%
C:\Program Files\Java\jdk1.8.0_171

已尝试的解决方法

  • 移除pom插件中的release配置
  • 执行mvn spring-boot:run -X,未获取有效信息

解决方案

核心问题

Spring Boot 3.2.0最低要求Java 17,不再兼容Java 8;同时你使用的maven-compiler-plugin 3.11.0已废弃1.8这种版本格式,需用8,但核心矛盾是Spring Boot版本与JDK版本不匹配。

方案一:升级JDK到17+

  1. 安装JDK 17或更高版本
  2. 更新系统JAVA_HOME指向新JDK路径
  3. 修改pom.xml中的Java版本配置:
    <properties>
        <java.version>17</java.version>
    </properties>
    
  4. 移除手动配置的maven-compiler-plugin(Spring Boot父pom会自动管理编译插件版本及参数)

方案二:降级Spring Boot版本到2.7.x

Spring Boot 2.7.x是支持Java 8的最后一个稳定系列,修改pom.xml的父依赖版本:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.18</version>
    <relativePath/>
</parent>

此方案可保留Java 8环境,无需修改java.version配置,手动添加的maven-compiler-plugin也可正常使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 20:35:12