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

如何解决Gradle gatlingRun任务的java.lang.ClassNotFoundException: BasicSimulation?

Fixing ClassNotFoundException: BasicSimulation with Gradle Gatling Plugin

Let's break down what's going wrong with your setup and fix it step by step.

The Root Causes in Your Current Config

Your configuration has a couple of key issues preventing Gatling from locating your simulation class:

  • You're repurposing the main source set for Gatling simulations instead of using the dedicated source set the plugin provides.
  • You're using generic compile dependencies instead of the Gatling-specific configuration that aligns with the plugin's classpath management.
  • Your simulation inclusion pattern might not be targeting the right file location.

Step-by-Step Correct Configuration

Here's the adjusted build.gradle that will resolve the class loading error:

plugins {
    id "com.github.lkishalmi.gatling" version "3.0.2"
    id 'scala'
}

repositories {
    mavenCentral()
}

// Use the dedicated Gatling source set instead of overriding main
sourceSets {
    gatling {
        scala {
            srcDirs = ['src/gatling/simulations'] // Match your actual simulation directory
        }
    }
}

dependencies {
    // Use gatlingCompile for Gatling-specific dependencies
    gatlingCompile "io.gatling:gatling-core:3.0.2"
    gatlingCompile "io.gatling:gatling-http:3.0.2"
    gatlingCompile "io.gatling.highcharts:gatling-charts-highcharts:3.0.2"
    gatlingCompile "org.scala-lang:scala-library:2.11.8"
}

gatling {
    // Target your specific simulation file (use **/ to handle nested packages if needed)
    simulations = {
        include "**/BasicSimulation.scala"
    }
}

Key Changes Explained

  • Dedicated Gatling Source Set: The plugin provides a gatling source set out of the box, so there's no need to overwrite the main source set. This ensures your simulation code is compiled into a classpath that the Gatling task can reliably find.
  • Gatling-Specific Dependencies: Using gatlingCompile instead of generic compile ensures dependencies are added to the Gatling task's classpath, not your main application's classpath.
  • Precise Simulation Matching: The **/BasicSimulation.scala pattern ensures Gradle finds your simulation file even if it's nested in subdirectories (i.e., part of a package). If your simulation is in a specific package like com.yourteam.loadtests, you can narrow it down to **/com/yourteam/loadtests/BasicSimulation.scala.

How to Run the Task

You don't need to explicitly run compileScala first — the gatlingRun task already depends on gatlingCompileScala, so it will automatically compile your simulations before execution. Just run:

gradlew gatlingRun -Dorg.gradle.java.home=C:\Users\saber.alex\Documents\jre1.8.0_91

To run your specific simulation directly (avoiding selection prompts), use:

gradlew gatlingRun-BasicSimulation -Dorg.gradle.java.home=C:\Users\saber.alex\Documents\jre1.8.0_91

Quick Verification Checks

Before re-running, double-check these details:

  • Ensure BasicSimulation.scala is actually in the src/gatling/simulations directory (or the path you specified in the source set).
  • Confirm there are no typos in the class name (e.g., BasicSimulaton instead of BasicSimulation).
  • Verify your Scala version (2.11.8) is compatible with Gatling 3.0.2 (it is — Gatling 3.x uses Scala 2.11.x for this version).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:11:42