IntelliJ中Spark Submit运行JAR报Class not found及Drools程序JAR运行异常咨询
Hey there! Let’s work through your two frustrating problems—these are super common when moving from IDE testing to production/JAR deployments, so I’ve got actionable fixes for each scenario.
1. Drools App Runs in IDE but Fails When Executing the JAR Directly
Most of the time, this boils down to missing dependencies or incorrect resource packaging. Here’s what to check:
Missing Drools Dependencies in the JAR
IDEs automatically pull in all your Maven/Gradle dependencies, but a standard JAR won’t include them by default. You need to build a "fat JAR" (or uber JAR) that bundles all required libraries.- For Maven: Use the
maven-shade-pluginin yourpom.xml. Make sure to configure it to include Drools core libraries likedrools-core,drools-compiler, andkie-api. Also, watch for dependency conflicts (e.g., ASM versions used by Drools vs. other libraries)—use the plugin’s relocation feature to rename conflicting packages if needed. - For Gradle: Use the
shadowplugin, which simplifies fat JAR creation. Addid 'com.github.johnrengelman.shadow' version 'x.x.x'to your plugins block, then run./gradlew shadowJarto build the bundled JAR.
- For Maven: Use the
Rule Files/Configuration Not Packaged Correctly
If your.drlrule files orkmodule.xml(Drools configuration) aren’t in the right place in the JAR, theKieContainerwon’t initialize.- Ensure
.drlfiles are in thesrc/main/resourcesdirectory (or a subfolder under it). When you build the JAR, check that these files exist underresources/in the unpacked JAR. - Verify
kmodule.xmlis located atsrc/main/resources/META-INF/kmodule.xml—this is the default location Drools looks for to load the Kie module.
- Ensure
Check the Full Error Stack Trace
Run the JAR withjava -jar your-app.jarand copy the entire error log. Often, the root cause is buried in the stack—like a missing class from a transitive Drools dependency, or a rule file path that works in the IDE but not in the JAR.
2. Class Not Found Error When Running via Spark Submit in IntelliJ
Spark’s classloading model is a bit tricky—here’s why you’re hitting this and how to fix it:
Drools Dependencies Aren’t Available to Spark Executors
When you submit a JAR to Spark, the driver and executors run in separate JVMs. If your JAR doesn’t include Drools dependencies, or Spark isn’t told to pull them in, executors will throw ClassNotFound exceptions.- Option 1: Bundle Dependencies in Your Fat JAR
Use the same fat JAR approach from the first section. Just make sure your Spark dependencies are marked asprovidedin Maven/Gradle (since Spark clusters already have these libraries) to avoid conflicts. - Option 2: Pass Dependencies via Spark Submit Flags
If you don’t want a fat JAR, use the--jarsflag to specify Drools JARs:
Or usespark-submit --jars drools-core-x.x.x.jar,drools-compiler-x.x.x.jar,kie-api-x.x.x.jar --class com.your.package.MainClass your-app.jar--packagesto pull dependencies directly from Maven Central:spark-submit --packages org.drools:drools-core:x.x.x,org.drools:drools-compiler:x.x.x --class com.your.package.MainClass your-app.jar
- Option 1: Bundle Dependencies in Your Fat JAR
Incorrect Main Class Path
Double-check that the--classparameter in your Spark Submit configuration matches the full qualified name of your main class (e.g.,com.example.DroolsSparkApplication). IntelliJ sometimes auto-fills this incorrectly, so verify against your project’s package structure.Classloader Conflicts
Spark’s default classloader might prioritize its own libraries over your app’s, causing Drools classes to not be found. Try adding these configuration flags to your Spark Submit command:--conf spark.driver.userClassPathFirst=true --conf spark.executor.userClassPathFirst=trueNote: This can cause other conflicts, so test it thoroughly before using in production.
Quick Debugging Tips
- Unpack your JAR with
unzip your-app.jarand inspect theBOOT-INF/lib(for Spring Boot) or root directory to confirm Drools classes and rule files are present. - In IntelliJ, enable verbose logging for Spark Submit to see exactly which classes are being loaded (or not loaded).
内容的提问来源于stack exchange,提问作者Suhita Goswami

