A quick guide to show you how to run a Spring batch job with CommandLineJobRunner.
1. Spring Batch Job Example
A simple job.
<?xml version="1.0" encoding="UTF-8"?>
<beans ...
<import resource="../config/context.xml"/>
<job id="readJob" xmlns="http://www.springframework.org/schema/batch">
<step id="step1">
<tasklet>
<chunk reader="flatFileItemReader"
writer="flatFileItemWriter" commit-interval="1" />
</tasklet>
</step>
</job>
<!-- ... -->
</beans>
2. Package Project
Use Maven to package your project into a single jar file – target/your-project.jar, and copy all the dependencies into target/dependency-jars/.
<!-- ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/dependency-jars/
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
$ mvn package
3. CommandLineJobRunner example
Usage :
CommandLineJobRunner jobPath <options> jobIdentifier (jobParameters)
To run above spring batch job, type following command :
$ java -cp "target/dependency-jars/*:target/your-project.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner spring/batch/jobs/job-read-files.xml readJob
For jobParameters, append to the end of the command :
$ java -cp "target/dependency-jars/*:target/your-project.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner spring/batch/jobs/job-read-files.xml readJob file.name=testing.cvs
To run it on a schedule, normally, you can copy above commands into a .sh file, and run it with any scheduler commands, like cron in *nix. Refer to this example – Add Jobs To cron Under Linux.
P.S When batch job is running under system scheduler, make sure it can locate your project’s classpath.
This example is good, but there are many errors with your downloadable source:
pom is describing different finalName than your commandline usage is expecting
Commandline usage is using : to seperate classpath instead of ;
Commandline usage is using the wrong id for the batch job.
Here is correct command line
java -cp “target/dependency-jars/*;target/spring-batch.jar” org.springframework.batch.core.launch.support.CommandLineJobRunner spring/batch/jobs/job-read-files.xml readMultiFileJob
Matt,
Your correction for the command helped me a lot. Thank you very much!! Besides, mkyong still presented a really good example! Now, I’m fighting for how to run it by Spring Batch Admin!
I just figured it out ‘;’ is for linux, ‘:’ is for windows
Hello I have a project in sprign batch, but for architectural reasons I need to use a JDNI, the jobs I want to start with commands generating a Jar, I have been looking but I can not find something that could be of help, any ideas?
Thanks for the space
Do you have any video tutorials on spring batches and schedulers?
How we can schedule a cron job through command line ? How can I run same project once and same project in every 5 mins, by giving flags at command line ?
Another way to run batch job, is that we write the running code in a App.java class. We compile it. And we use java -cp to call the App.class.
Matt Kindzerske , you´re completly right, thanks helps alot!
Hi yong,
I am unable to run the batch using command line. Need your help. Let me know convenient time to reach you.
[email protected]/[email protected]/+91 9989764000