如何解决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
mainsource set for Gatling simulations instead of using the dedicated source set the plugin provides. - You're using generic
compiledependencies 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
gatlingsource set out of the box, so there's no need to overwrite themainsource set. This ensures your simulation code is compiled into a classpath that the Gatling task can reliably find. - Gatling-Specific Dependencies: Using
gatlingCompileinstead of genericcompileensures dependencies are added to the Gatling task's classpath, not your main application's classpath. - Precise Simulation Matching: The
**/BasicSimulation.scalapattern 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 likecom.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.scalais actually in thesrc/gatling/simulationsdirectory (or the path you specified in the source set). - Confirm there are no typos in the class name (e.g.,
BasicSimulatoninstead ofBasicSimulation). - 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
相关产品推荐
相关产品推荐

