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

Java11下执行mvn clean install抛出NoClassDefFoundError: java/sql/Time异常

问题根因

报错本质是Java 11已将java.sql模块从默认加载的基础模块中移除,你项目依赖的旧版gherkin内嵌的shaded版gson未适配Java 9+的模块系统。Eclipse运行测试时默认加载了全部JDK模块所以无异常,Maven执行mvn clean install触发测试时默认未加载java.sql模块,就会出现类找不到的异常。

解决方案
  • 方案1:升级依赖适配Java 11
    优先升级gherkin到适配Java 11的版本,即可解决内嵌gson的兼容问题:
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>gherkin</artifactId>
        <version>26.2.0</version>
    </dependency>
    
    如果暂时无法升级gherkin,也可以显式声明独立的高版本gson(2.8.9及以上)覆盖传递依赖:
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.10.1</version>
    </dependency>
    
  • 方案2:为Maven Surefire插件添加模块加载参数
    如果不方便调整依赖,可直接修改pom.xml中maven-surefire-plugin的配置,手动指定加载java.sql模块:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.1.2</version>
        <configuration>
            <argLine>--add-modules java.sql</argLine>
        </configuration>
    </plugin>
    
  • 方案3:校验Maven的JDK配置一致性
    执行mvn -v确认Maven使用的JDK版本和Eclipse项目配置的Java 11一致,如果存在版本 mismatch,可在pom.xml中显式锁定编译和运行的JDK版本:
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 18:54:03