Spring Batch CommandLineJobRunner类找不到错误求助(Gradle+Windows)
Hey, I’ve dealt with this exact issue before when working with Spring Batch and Gradle on Windows— let’s break down how to fix it step by step!
1. 先搞清楚Spring Boot Fat Jar的特殊性
Your BatchExample.jar is likely a Spring Boot fat jar— these jars package dependencies inside BOOT-INF/lib, and use Spring Boot’s own JarLauncher as the entry point. When you use java -cp directly, the default class loader doesn’t look inside BOOT-INF/lib, so it can’t find CommandLineJobRunner.
Try using the Spring Boot jar launch command instead:
java -jar build/libs/BatchExample.jar org.springframework.batch.core.launch.support.CommandLineJobRunner JobConfigClass jobName
This lets Spring Boot’s loader handle all the internal dependencies, so the class should be found correctly.
2. Build a standard executable jar (if you don’t need Spring Boot’s fat jar)
If you prefer to use the java -cp approach, adjust your Gradle config to build a standard jar that includes all dependencies:
Add this to your build.gradle:
plugins { id 'java' } jar { manifest { attributes 'Main-Class': 'org.springframework.batch.core.launch.support.CommandLineJobRunner' } // Package all runtime dependencies into the jar from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } }
Rebuild with:
./gradlew clean build
Then run your original command (no need for lib/* now, since all dependencies are in the jar):
java -cp "build/libs/BatchExample.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner JobConfigClass jobName
3. Double-check you have the right dependency
You mentioned having spring-core, but CommandLineJobRunner lives in spring-batch-core, not spring-core. Make sure your build.gradle includes this dependency:
dependencies { implementation 'org.springframework.batch:spring-batch-core:4.3.7.RELEASE' // Match your spring-core version implementation 'org.springframework:spring-core:4.3.7.RELEASE' // Don't forget other required dependencies like spring-context, spring-jdbc, etc. }
4. Fix Windows classpath syntax quirks
Windows uses ; as the classpath separator, but quote handling can be finicky in PowerShell. Try running the command in Command Prompt instead, or adjust the path formatting:
java -cp build\libs\BatchExample.jar;lib\* org.springframework.batch.core.launch.support.CommandLineJobRunner JobConfigClass jobName
Also confirm the lib directory actually contains all your project’s dependency jars.
内容的提问来源于stack exchange,提问作者JainAnk

