title: Batch job fails because of JobExecutionAlreadyRunningException: A job execution for this job is already running
body: <p>How to stop batch job from the main method ? I am running the batch job from my method and now its giving me the below error. Any help on how to kill and stop it? </p>
  
  <pre><code>A job execution for this job is already running: JobInstance: id=18380, version=0, Job=[myFileGenerationJob]
  org.springframework.batch.core.repository.JobExecutionAlreadyRunningException: A job execution for this job is already running: JobInstance: id=18380, version=0, Job=[myFileGenerationJob]
  Done
      at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:120)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:280)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:172)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
      at com.sun.proxy.$Proxy3.createJobExecution(Unknown Source)
      at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:125)
      at com.mastercard.ess.eds.CustomerFileGenerationApp.main(CustomerFileGenerationApp.java:31)
  </code></pre>
  
  <p>I am using the following code - </p>
  
  <pre><code>public class CustomerFileGenerationApp {
  
      public static void main(String[] args) {
          PropertyConfigurator.configure("src/main/resources/log4j.properties");
          String[] springConfig = { "../META-INF/spring/customer-file-generation-job.xml"};
  
          ApplicationContext context = new FileSystemXmlApplicationContext(springConfig);
  
          JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
          Job job = (Job) context.getBean("customerFileGenerationJob");
  
          JobParameters jobParameters = new JobParametersBuilder().addString("runMode", "ABCD").toJobParameters();
  
          try {
              JobExecution execution = jobLauncher.run(job, jobParameters);
              System.out.println("Exit Status : " + execution.getStatus());
          } catch (Exception e) {
              System.out.println(e.getMessage());
              e.printStackTrace();
          }
  
          System.out.println("Done");
      }
  
  }
  </code></pre>