'Failed to execute CommandLineRunner - Spring Batch

Hi I am very new to Spring batch and I am getting the following exception which I am not able to resolve:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at com.demo.BatchDemo.main(KnpBatchApplication.java:16) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_72]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_72]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_72]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_72]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.1.RELEASE.jar:2.0.1.RELEASE]
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653) ~[na:1.8.0_72]
at java.util.ArrayList.get(ArrayList.java:429) ~[na:1.8.0_72]
at org.springframework.batch.core.JobParametersBuilder.getNextJobParameters(JobParametersBuilder.java:265) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:162) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:179) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:134) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:128) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
... 10 common frames omitted

My Code here:

    @SpringBootApplication
    public class BatchDemo {
    public static void main(String args[])
    {
        SpringApplication.run(BatchDemo.class);
    }
}

I was able to resolve it by adding exclude = BatchAutoConfiguration.class. But I would like to find the actual cause and fix it.

I am not passing any commandline arguments and this exception doesn't occur all the time.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo</groupId>
<artifactId>demo_batch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo_batch</name>
<url>http://maven.apache.org</url>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>



Solution 1:[1]

When you use @SpringBootApplication, Spring's autoConfiguration is enabled by default. Since you have spring batch dependencies loaded in your classpath, Spring Batch AutoConfiguration is enabled as well.

During application startup, Spring Batch's autoconfiguration (BatchAutoConfiguration) creates a Runner and it runs all the jobs defined in your BatchConfig.

You can disable this behavior by either setting spring.batch.job.enabled property to false in your application properties or by simply excluding the Autoconfiguration for Batch just like as you did.

Please refer here and here for more info.

Solution 2:[2]

For anyone who wants their batch jobs to actually execute, and is encountering this error, try checking if there is any inconsistency in the records in your Spring batch metadata tables.

To get the next JobParameters, Spring Batch searches for the latest BATCH_JOB_INSTANCE with the same JOB_NAME as your job, then looks for the matching BATCH_JOB_EXECUTION record in the database (linked by JOB_INSTANCE_ID).

We had an in-house task running which deleted records from BATCH_JOB_EXECUTION but, due to an error, did not delete the matching record from BATCH_JOB_INSTANCE.

In one environment, it had been more than 3 months since we had executed any jobs, so we had no BATCH_JOB_EXECUTION records anymore, but plenty of BATCH_JOB_INSTANCE records. We repeatedly threw the stack trace listed in this question, across multiple different Spring Batch applications, and could not find a solution anywhere online.

We resolved the issue by

  1. Running SQL to delete from BATCH_JOB_INSTANCE where there is no matching record in BATCH_JOB_EXECUTION - this unblocked the job in the short term; and,
  2. Replacing our purge task with a job using spring-batch-toolkit; it provides a RemoveSpringBatchHistoryTasklet that deletes the records correctly.

Solution 3:[3]

I got this error and it was because I was reading in files from a folder that followed a general naming convention and type. I by mistake left an image file in the same directory and my code could not process it and this error was thrown.

Solution 4:[4]

If you write any syntax errors, or spelling mistakes it also causes the above error. For example, I wrote it like this but there are some syntax errors in my SQL query:

String sql = "insert into Student values(1,'abhilash',india");

Correct one:

String sql = "insert into Student values(1,'abhilash','india')";

Now, the code ran fine. Just check your queries once.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ram
Solution 2 Corwin Newall
Solution 3 Barbara
Solution 4 Ethan