java lang illegalstateexception failed to execute commandlinerunner
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 :::
@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
4.0.0
demo
demo_batch
0.0.1-SNAPSHOT
jar
demo_batch
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
org.postgresql
postgresql
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.batch
spring-batch-core
org.springframework
spring-core
org.springframework
spring-context
org.springframework.boot
spring-boot-starter-batch
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
junit
junit
test
org.springframework.boot
spring-boot-starter-test
test
org.springframework.batch
spring-batch-test
test
The stack trace indicates that the java.lang.IndexOutOfBoundsException
is occurring when trying to retrieve job parameters while executing the CommandLineRunner
for Spring Batch. The root cause seems to be that the JobParametersBuilder
is not able to retrieve job parameters from an empty list.
To resolve this issue, you can check if there are job parameters available before trying to retrieve them. You can use the JobParameters
to pass parameters to your job, and it’s essential to ensure that the parameters are available before execution.
Here’s an updated version of your main class that checks for job parameters:
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;@SpringBootApplication(exclude = BatchAutoConfiguration.class)
public class BatchDemo {
public static void main(String args[]) {
// Check if job parameters are available before running the application
JobParameters jobParameters = new JobParametersBuilder().toJobParameters();
if (jobParameters.getParameters().isEmpty()) {
SpringApplication.run(BatchDemo.class);
}
}
}
By using JobParametersBuilder().toJobParameters()
, we create an empty set of job parameters, and then we check if the parameters
map is empty. If it is empty, we do not execute the SpringApplication.run()
. This will prevent the IndexOutOfBoundsException
from occurring when there are no job parameters available.
Please note that this solution assumes that there is a specific scenario in your application where you expect job parameters to be available. If the absence of job parameters is expected in other scenarios, you can further customize the condition accordingly.
Remember to handle job parameters appropriately when running your Spring Batch job to pass any required parameters during execution.