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

Java 11+Maven项目Spring类文件版本不匹配错误如何修复

问题

我在IntelliJ IDEA中使用Java 11和Maven开发,编写了如下JacksonInjectExample类:

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.nio.file.Paths;

@JsonTest
public class JacksonInjectExample {

    private static final String filePath = "test.json";

    private static final String JSON = String.format(String.valueOf(Paths.get(filePath)));

    @Autowired
    private JacksonTester<OHStepTrackingNotification> jacksonTester;

    @Configuration
    public static class TestConfiguration {...}

    @BeforeClass
    static public void setSystemProperties() throws IOException {...}

    @Test
    public void test() throws IOException {...}
}

编译时导入Spring相关包失败,涉及的导入语句:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

报错信息:

java: cannot access org.springframework.beans.factory.annotation.Autowired
  bad class file: /C:/Users/<username>/.m2/repository/org/springframework/spring-beans/6.0.2/spring-beans-6.0.2.jar!/org/springframework/beans/factory/annotation/Autowired.class
    **class file has wrong version 61.0, should be 55.0**
    Please remove or make sure it appears in the correct subdirectory of the classpath.

当前环境:

  • JAVA_HOME配置为Java 11,java -version输出:openjdk version "11.0.17" 2022-10-18 LTS
  • 已尝试操作:
    • 更新JDK至Java 11
    • 在IntelliJ中配置项目使用Java 11
    • 删除.idea文件夹
    • 清除缓存

求解决版本不匹配的方法。

解决方案

这个问题的核心是Spring Boot 6.x版本要求使用Java 17+,而你当前用的是Java 11,导致依赖的spring-beans-6.0.2.jar是用Java 17(版本号61.0)编译的,无法在Java 11(版本号55.0)环境下运行。

有两个可行的解决方向,二选一即可:

方案1:降级Spring Boot版本到兼容Java 11的版本

Spring Boot 2.7.x是最后一批支持Java 11的稳定版本,修改项目pom.xml中的Spring Boot父依赖版本:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!-- 替换为2.7.x的最新稳定版本,比如2.7.15 -->
    <version>2.7.15</version>
    <relativePath/>
</parent>

修改后执行mvn clean install,让Maven重新下载适配Java 11的Spring依赖包。

方案2:升级JDK到Java 17或更高版本

如果项目允许升级JDK,可按以下步骤操作:

  1. 在IntelliJ中配置:
    • 打开File > Project Structure,将Project SDK和Project language level设置为Java 17
    • 进入Settings > Build, Execution, Deployment > Build Tools > Maven > Runner,设置JRE为Java 17
  2. 修改pom.xml中的编译版本配置:
<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
</properties>

完成后重新编译项目即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 14:55:22