title: Resource is not loading in executable Spring boot jar - How executable jar loads resources?
body: <p>I am trying to run executable boot spring jar based on documentation provided <a href="http://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html" rel="noreferrer">here</a>. I am expecting the resources will be copied to the executable jar on running</p>
  
  <pre><code>mvn clean package
  </code></pre>
  
  <p>Here is how I am running the .jar under my project folder</p>
  
  <pre><code>./my-application.jar
  </code></pre>
  
  <p>In my project I have a batch process that is running on startup where I am trying to load a resource defined under </p>
  
  <pre><code>src/main/resources/batch/request_customers.csv
  </code></pre>
  
  <p>Here is how I am loading the resource
  <strong>application.properties</strong></p>
  
  <pre><code>data.customers.input=classpath:batch/request_customers.csv
  </code></pre>
  
  <p><strong>class</strong></p>
  
  <pre><code>import org.springframework.util.ResourceUtils;
  @Component
  public class DataManager {
  
      @Value("${data.customers.input}")
      private String usersExistingData;
  
      public File jsonCustomerData() throws FileNotFoundException {
          return  ResourceUtils.getFile(usersExistingData);
      }
  
  
  }
  </code></pre>
  
  <p><strong>error log</strong></p>
  
  <pre><code>s]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.groupon.batch.CustomerItemReader]: Factory method 'reader' threw exception; nested exception is java.io.FileNotFoundException: class path resource [batch/request_customers.csv] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/xxx/projects/myproject/target/myproject-0.0.2-SNAPSHOT.jar!/BOOT-INF/classes!/batch/request_customers.csv; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'job' defined in class path resource [com/mycompany/batch/BatchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Job]: Factory method 'job' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step1' defined in class path resource [com/mycompany/batch/BatchConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.core.Step]: Factory method 'step1' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reader' defined in class path resource 
  </code></pre>
  
  <p><strong>I also tried following path</strong> </p>
  
  <pre><code>data.customers.input=src/resources/batch/request_customers.csv
  data.customers.input=batch/request_customers.csv
  </code></pre>
  
  <p>When I run application using my IDE and spring plugin to resources are loaded without any issues.But fails when I run executable jar using ./my-application.jar </p>
  
  <ul>
  <li>The question is how to make sure all resources are copies to jar when
  I package it ?</li>
  <li>Do I need to do some other configuration to tell maven to copy resources as they are structured ?</li>
  <li>How I can check if resources are packages into jar ? I can not extract jar for some reasons ?</li>
  </ul>
  
  <p>It would be great to know how exactly I can build a executable jar that basically works like an <em>executable</em>.</p>
  
  <p><strong>UPDATE</strong>
  the file exists in the jar that should be loaded. I am not loading from an external file path.</p>
  
  <pre><code>BOOT-INF/classes/batch/request_customers.csv
  </code></pre>
  
  <p><strong>Here is the solution</strong></p>
  
  <p>When you build a jar file and deploy to a path then you need to treat resources as on file system - relative paths will not magically load resources, which is what I was expecting. You have to open as stream and read them. Here is one way to do that </p>
  
  <p>StringBuffer buffer = new StringBuffer();</p>
  
  <pre><code>BufferedReader inputStream =
          new BufferedReader(new InputStreamReader(resourceloader.getResource(file).getInputStream()));
  
  String line = null;
  
  while ((line = inputStream.readLine()) != null){
      buffer.append(line);
  }
  </code></pre>