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

Java 8环境搭建Cucumber项目遇类版本兼容错误求助

错误原因分析
  • Spring Boot版本不兼容:你使用的spring-boot-starter-web 3.1.3属于Spring Boot 3.x系列,该系列最低要求Java 17,它引入的commons-logging相关类是用Java 17编译的(class文件版本61.0),而Java 8仅能识别最高52.0的class文件,直接触发UnsupportedClassVersionError。
  • Cucumber依赖版本混乱:同时混用了新groupId(io.cucumber)的cucumber-core 7.5.0和旧groupId(info.cukes)的cucumber-java/cucumber-testng 1.2.5,新旧版本依赖冲突;且Cucumber 7.x系列最低要求Java 11,本身不兼容Java 8。
  • 依赖链冲突:高版本Spring Boot引入的Apache HttpClient、commons-logging等依赖,与项目中指定的低版本依赖(如rest-assured 3.3.0)存在版本不匹配,进一步加剧运行时错误。
Java 8环境下的解决办法

1. 替换Spring Boot为Java 8兼容的2.x版本

Spring Boot 2.x完全支持Java 8,将spring-boot-starter-web替换为2.x长期支持版本,比如2.7.17:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.7.17</version>
</dependency>

2. 统一Cucumber依赖为Java 8兼容版本

移除所有info.cukes开头的Cucumber依赖,替换为io.cucumber的6.x系列(Cucumber 6.x是最后支持Java 8的大版本),保持版本一致:

<!-- Cucumber核心依赖 -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-core</artifactId>
    <version>6.11.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>6.11.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>6.11.0</version>
    <scope>test</scope>
</dependency>

3. 调整Extent Report适配器版本

原cucumber-extentsreport 3.1.1仅支持Cucumber 4.x,替换为支持Cucumber 6.x的版本5.0.1:

<dependency>
    <groupId>com.vimalselvam</groupId>
    <artifactId>cucumber-extentsreport</artifactId>
    <version>5.0.1</version>
    <scope>test</scope>
</dependency>

4. 修正TestNG为正式稳定版

将TestNG的RC版本替换为正式稳定版6.14.3:

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
    <scope>compile</scope>
</dependency>

5. 保留兼容Java 8的其他依赖

rest-assured 3.3.0、junit 4.12、extentreports 4.0.9、commons-io 2.7本身兼容Java 8,可继续使用。

最终整理后的依赖清单

<dependencies>
    <!-- Cucumber依赖(Java8兼容的6.x版本) -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-core</artifactId>
        <version>6.11.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>6.11.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>6.11.0</version>
        <scope>test</scope>
    </dependency>

    <!-- Rest Assured -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>3.3.0</version>
        <scope>test</scope>
    </dependency>

    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <!-- Extent Reports -->
    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>4.0.9</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.vimalselvam</groupId>
        <artifactId>cucumber-extentsreport</artifactId>
        <version>5.0.1</version>
        <scope>test</scope>
    </dependency>

    <!-- TestNG -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.14.3</version>
        <scope>compile</scope>
    </dependency>

    <!-- Spring Boot(Java8兼容的2.x版本) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.7.17</version>
    </dependency>

    <!-- Commons IO -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.7</version>
    </dependency>
</dependencies>

验证步骤

  1. 执行mvn clean install清理并重新构建依赖,确认无版本冲突。
  2. 运行Cucumber测试,验证UnsupportedClassVersionError消失,项目正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 06:05:58