Run Spring batch job with CommandLineJobRunner

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="">
      <step id="step1">
		<chunk reader="flatFileItemReader" 
                          writer="flatFileItemWriter" commit-interval="1" />

	<!-- ... -->

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/.


  <!-- ... -->

$ 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" 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" spring/batch/jobs/job-read-files.xml readJob

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.

Download Source Code

Download it – KB)


  1. CommandLineJobRunner JavaDoc
  2. How To Create A Jar File With Maven
  3. Spring Batch Hello World – In Memory
author image


Founder of, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by


newest oldest most voted
Mike Dev
Mike Dev

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 ?

peng li
peng li

Another way to run batch job, is that we write the running code in a class. We compile it. And we use java -cp to call the App.class.


Matt Kindzerske , you´re completly right, thanks helps alot!

Matt Kindzerske
Matt Kindzerske

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” spring/batch/jobs/job-read-files.xml readMultiFileJob

peng li
peng li


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!

peng li
peng li

I just figured it out ‘;’ is for linux, ‘:’ is for windows

SrinivasaRao Gurram
SrinivasaRao Gurram

Hi yong,
I am unable to run the batch using command line. Need your help. Let me know convenient time to reach you. 9989764000