TPPZ
TPPZ

Reputation: 4891

Running Spring Batch jobs from the command line

I don't know how to call a Job defined in Spring Batch using CommandLineJobRunner, documentation details are not enough for me.

I've followed the Spring Batch official guide to write Jobs in Spring Batch using Java annotations e.g. @EnableBatchProcessing because I wanted to avoid XML configuration files for the description of the job, the steps, etc.

So far I have:

On the Maven side I am currently using some plugins:

I think I've configured all the things I need to configure, however after having a Maven BUILD SUCCESS I am not able to run the job from the command line:

java -cp ./target/JAR_FILE_NAME.jar org.springframework.batch.core.launch.support.CommandLineJobRunner com.package.bla.bla.ClassContainingTheBatchConfiguration nameOfTheJob

Is throwing IOException due to the java.io.FileNotFoundException regarding com.package.bla.bla.ClassContainingTheBatchConfiguration. How should I specify the parameters in the command line in order to get the Job executed?

Upvotes: 11

Views: 24526

Answers (3)

andrelucky
andrelucky

Reputation: 1

dont use spring.batch.job.enabled=false then run using java -jar [jar-files] --spring.batch.job.names=[job-name]

Upvotes: 0

Emerson Farrugia
Emerson Farrugia

Reputation: 11353

If the first argument to the CommandLineJobRunner is your @Configuration FQCN instead of a resource path, the ClassPathXmlApplicationContext constructor that's called from the CommandLineJobRunner's start() method will break.

int start(String jobPath, String jobIdentifier, String[] parameters, Set<String> opts) {

    ConfigurableApplicationContext context = null;

    try {
        context = new ClassPathXmlApplicationContext(jobPath);

If you've already written a class with a main(), that replaces the CLJR, you shouldn't be passing CLJR as the class name in the command line. Pass that instead.

Upvotes: 1

Dave Syer
Dave Syer

Reputation: 58094

If you are already using SpringApplication from Spring Boot, why not finish the job and use @EnableAutoConfiguration as well, and also the Maven plugin (see for example this guide)? That way you will get something working pretty quickly and you can always add your own features later.

Upvotes: 2

Related Questions