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.

Answer from Dave Syer on Stack Overflow
🌐
Spring
docs.spring.io › spring-batch › reference › job › running.html
Running a Job :: Spring Batch Reference
The default implementation used by the job runner is the SimpleJvmExitCodeMapper that returns 0 for completion, 1 for generic errors, and 2 for any job runner errors such as not being able to find a Job in the provided context. If anything more complex than the three values above is needed, a custom implementation of the ExitCodeMapper interface must be supplied by setting it on the CommandLineJobOperator. Historically, offline processing (such as batch jobs) has been launched from the command-line, as described earlier.
🌐
Medium
medium.com › @gangaa › a-simple-spring-boot-batch-service-for-a-task-to-execute-from-command-line-ef64d88be778
A simple Spring boot batch service for a task to execute from command line | by Venkata Kopparapu | Medium
July 15, 2024 - Now to use the app for command line and terminate the spring boot application after finishing its job. package com.vk.batch.commandline.batch_commandline; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class BatchCommandlineApplication { public static void main(String[] args) { System.exit(SpringApplication.exit(SpringApplication.run(BatchCommandlineApplication.class, args))); } }
🌐
Spring
docs.spring.io › spring-batch › docs › 1.0.x › spring-batch-docs › reference › html › execution.html
Chapter 4. Configuring and Executing A Job
This example is overly simplistic, since there are many more requirements to a run a batch job in Spring Batch in general, but it serves to show the two main requirements of the CommandLineJobRunner: Job and JobLauncher · When launching a batch job from the command-line, it is often from an ...
🌐
Java Development Journal
javadevjournal.com › home › spring batch › spring batch job configuration
Configuring and Running Spring Batch Job
September 27, 2020 - Find the Job based on command line arguments. From the ApplicationContext, use the JobLauncher to launch the job. Everything depends on the arguments being passed to it. CommandLineJobRunner arguments are: JobPath Java or XML config used as ...
🌐
Dinesh on Java
dineshonjava.com › home › configuring and running a job in spring batch
Configuring and Running a Job in Spring Batch - Dinesh on Java
December 23, 2017 - Both can be contained within the same context or different contexts. For example, if launching a job from the command line, a new JVM will be instantiated for each Job, and thus every job will have its own JobLauncher. However, if running ...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › enterprise java › spring › batch
Spring Batch CommandLineJobRunner Example - Java Code Geeks
February 13, 2019 - Lines 32,33 represent the test configuration. We need to configure the main class to CommandLineJobRunner or else it will pick our application class as the main class and directly execute our jobs.
Find elsewhere
🌐
Spring
docs.spring.io › spring-batch › docs › current › api › org › springframework › batch › core › launch › support › CommandLineJobRunner.html
CommandLineJobRunner (Spring Batch 5.2.5 API)
JobLocator to find a job to run. ... Launch a batch job using a CommandLineJobRunner. Creates a new Spring context for the job execution, and uses a common parent for all such contexts. No exception are thrown from this method, rather exceptions are logged and an integer returned through the ...
🌐
Spring
docs.spring.io › spring-batch › docs › 5.0.0 › reference › html › job.html
Configuring and Running a Job
November 23, 2022 - Spring Batch provides an implementation that serves this purpose: CommandLineJobRunner. Note that this is just one way to bootstrap your application. There are many ways to launch a Java process, and this class should in no way be viewed as definitive. The CommandLineJobRunner performs four tasks: Load the appropriate ApplicationContext. Parse command line arguments into JobParameters.
🌐
Spring
docs.spring.io › spring-batch › docs › 5.0.3 › reference › html › job.html
Configuring and Running a Job - Spring
Spring Batch provides an implementation that serves this purpose: CommandLineJobRunner. Note that this is just one way to bootstrap your application. There are many ways to launch a Java process, and this class should in no way be viewed as definitive. The CommandLineJobRunner performs four tasks: Load the appropriate ApplicationContext. Parse command line arguments into JobParameters.
🌐
SeedStack
seedstack.org › addons › spring-bridge › batch
Spring batch
Spring Batch jobs are run from a specific command-line handler providing the run-job command.
Top answer
1 of 4
27

Just set the "spring.batch.job.names=myJob" property. You could set it as SystemProperty when you launch your application (-Dspring.batch.job.names=myjob). If you have defined this property, spring-batch-starter will only launch the jobs, that are defined by this property.

2 of 4
13

To run the jobs you like from the main method you can load the the required job configuration bean and the JobLauncher from the application context and then run it:

@ComponentScan
@EnableAutoConfiguration
public class ApplicationWithJobLauncher {

    public static void main(String[] args) throws BeansException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, InterruptedException {

        Log log = LogFactory.getLog(ApplicationWithJobLauncher.class);

        SpringApplication app = new SpringApplication(ApplicationWithJobLauncher.class);
        app.setWebEnvironment(false);
        ConfigurableApplicationContext ctx= app.run(args);
        JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
        JobParameters jobParameters = new JobParametersBuilder()
            .addDate("date", new Date())
            .toJobParameters();  

        if("1".equals(args[0])){
            //addNewPodcastJob
            Job addNewPodcastJob = ctx.getBean("addNewPodcastJob", Job.class);          
            JobExecution jobExecution = jobLauncher.run(addNewPodcastJob, jobParameters);                   
        } else {
            jobLauncher.run(ctx.getBean("newEpisodesNotificationJob",  Job.class), jobParameters);   

        } 

        System.exit(0);
    }
}

What was causing my lots of confusion was that the second job were executed, even though the first job seemed to be "picked up" by the runner... Well the problem was that in both job's configuration file I used standard method names writer(), reader(), processor() and step() and it used the ones from the second job that seemed to "overwrite" the ones from the first job without any warnings... I used though an application config class with @EnableBatchProcessing(modular=true), that I thought would be used magically by Spring Boot :

@Configuration
@EnableBatchProcessing(modular=true)
public class AppConfig {

    @Bean
    public ApplicationContextFactory addNewPodcastJobs(){
        return new GenericApplicationContextFactory(AddPodcastJobConfiguration.class);
    }

    @Bean
    public ApplicationContextFactory newEpisodesNotificationJobs(){
        return new GenericApplicationContextFactory(NotifySubscribersJobConfiguration.class);
    }    

}

I will write a blog post about it when it is ready, but until then the code is available at https://github.com/podcastpedia/podcastpedia-batch (work/learning in progress)..

🌐
GitHub
github.com › jmformenti › spring-batch-rest-cli-example
GitHub - jmformenti/spring-batch-rest-cli-example: Example of execution same spring batch job via command line or REST Api · GitHub
batch-core implements a simple Spring batch job, computePi, which calculates Pi number with more or less precision in function of count parameter. You could implement more jobs here. batch-cli execute jobs via command line using Spring Boot.
Author   jmformenti
🌐
Stack Overflow
stackoverflow.com › questions › 46356926 › how-to-spring-boot-batch-job-running-manually-from-command-prompt
how to spring boot batch job running manually from command prompt - Stack Overflow
let us say : with out spring boot we can run like below: $ java -cp "target/dependency-jars/*:target/your-project.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner spring/batch/jobs/job-read-files.xml readJob
🌐
Spring
docs.spring.io › spring-batch › docs › 3.0.x › reference › html › configureJob.html
4. Configuring and Running a Job
This example is overly simplistic, since there are many more requirements to a run a batch job in Spring Batch in general, but it serves to show the two main requirements of the CommandLineJobRunner: Job and JobLauncher · When launching a batch job from the command-line, an enterprise scheduler is often used.