title: NoSuchJobException when running a job programmatically in Spring Batch
body: <p>I have a Job running on startup. I want to run this job programmatically at a particular point of my application, not when I start my app.</p>
  
  <p>When running on startup I have no problem, but I got a "NoSuchJobException" (<code>No job configuration with the name [importCityFileJob] was registered</code>) when I try to run it programmatically.</p>
  
  <p>After looking on the web, I think it's a problem related to JobRegistry, but I don't know how to solve it.</p>
  
  <p>Note : my whole batch configuration is set programmatically, I don't use any XML file to configure my batch and my job. That's a big part of my problem while I lack the examples...</p>
  
  <p>Here is my code to run the Job :</p>
  
  <pre><code>public String runBatch() {
      try {
          JobLauncher launcher = new SimpleJobLauncher();
          JobLocator locator = new MapJobRegistry();
          Job job = locator.getJob("importCityFileJob");
          JobParameters jobParameters = new JobParameters(); // ... ?
          launcher.run(job, jobParameters);
      } catch (Exception e) {
          e.printStackTrace();
          System.out.println("Something went wrong");
      }
      return "Job is running";
  }
  </code></pre>
  
  <p>My Job declaration :</p>
  
  <pre><code>@Bean
  public Job importCityFileJob(JobBuilderFactory jobs, Step step) {
      return jobs.get("importFileJob").incrementer(new RunIdIncrementer()).flow(step).end().build();
  }
  </code></pre>
  
  <p>(I tried to replace <code>importCityFileJob</code> by <code>importFileJob</code> in my runBatch method, but it didn't work)</p>
  
  <p>My BatchConfiguration file contains the job declaration above, a step declaration, the itemReader/itemWriter/itemProcessor, and that's all.
  I use the <code>@EnableBatchProcessing</code> annotation.</p>
  
  <p>I'm new to Spring Batch &amp; I'm stuck on this problem. Any help would be welcome.</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>Edit : I've solved my problem. I wrote my solution in the answers</p>