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

如何修复运行Gatling Jar时出现的java.lang.NoClassDefFoundError: scala/Serializable错误

运行Gatling Jar时遭遇java.lang.NoClassDefFoundError: scala/Serializable错误

问题详情

  • Java二进制路径:/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.275.b01-0.el6_10.x86_64/bin
  • 执行打包命令:mvn clean install -P buildTest
  • 运行Jar时抛出错误:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Serializable

生成的Jar包中缺失scala.Serializable类,对应的Maven buildTest 配置如下:

<profile>
    <id>buildTest</id>
    <activation>
        <activeByDefault>false</activeByDefault>
    </activation>
    <dependencies>
        <dependency>
            <groupId>io.gatling.highcharts</groupId>
            <artifactId>gatling-charts-highcharts</artifactId>
            <scope>runtime</scope>
            <version>${gratlin.charts}</version>
        </dependency>
        <dependency>
            <groupId>com.intuit.karate</groupId>
            <artifactId>karate-junit4</artifactId>
            <version>${karate.version}</version>
        </dependency>
        <dependency>
            <groupId>com.intuit.karate</groupId>
            <artifactId>karate-gatling</artifactId>
            <version>${karate.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>io.gatling</groupId>
                    <artifactId>gatling-commons</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.gatling</groupId>
                    <artifactId>gatling-charts</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.gatling</groupId>
                    <artifactId>gatling-charts-highcharts</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.gatling</groupId>
            <artifactId>gatling-app</artifactId>
            <version>${gratlin.app.version}</version>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.13.6</version>
        </dependency>
    </dependencies>
    <build>
        <testResources>
            <testResource>
                <directory>src/test/java</directory>
                <excludes>
                    <!--<exclude>**/*.java</exclude>-->
                </excludes>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>4.5.6</version>
                <executions>
                    <execution>
                        <id>scala-test-compile</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jvmArgs>
                        <jvmArg>-Xms512m</jvmArg>
                        <jvmArg>-Xmx2048m</jvmArg>
                    </jvmArgs>
                    <recompileMode>incremental</recompileMode>
                    <useZincServer>true</useZincServer>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.4.1</version>
                <configuration>
                    <finalName>${project.name}</finalName>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>io.gatling</groupId>
                <artifactId>gatling-maven-plugin</artifactId>
                <version>4.1.1</version>
                <configuration>
                    <simulationsFolder>src/test/scala</simulationsFolder>
                    <runMultipleSimulations>true</runMultipleSimulations>
                    <includes>
                        <include>orderServices.GetOrderSimulation</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

解决方法

  • 明确Scala依赖作用域
    为scala-library添加<scope>compile</scope>,确保它被纳入编译和打包流程:

    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.13.6</version>
        <scope>compile</scope>
    </dependency>
    
  • 调整maven-shade-plugin配置
    在插件的<configuration>中添加<artifactSet>,显式包含核心依赖,避免shade过程遗漏Scala类:

    <configuration>
        <!-- 保留原有配置 -->
        <artifactSet>
            <includes>
                <include>org.scala-lang:scala-library</include>
                <include>io.gatling:*</include>
                <include>com.intuit.karate:*</include>
            </includes>
        </artifactSet>
    </configuration>
    
  • 检查依赖树
    执行mvn dependency:tree -P buildTest,确认输出中org.scala-lang:scala-library存在,且未被间接排除。

  • 验证版本兼容性
    确认${gratlin.app.version}对应的Gatling版本支持Scala 2.13.6(Gatling 3.7+适配Scala 2.13系列),版本不匹配会导致类加载失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 10:52:26