title: Create a single executable JAR from a Spring Based Java Application
body: <p>I have a spring based application that does a component/package scan looking for packages within a certain namespace. The application runs perfectly in Eclipse and I'd like to create a single executable JAR to deploy to our various environments. </p>
  
  <p>I've tried various ways to get this to work but the only thing that's working is if I include the dependencies in a folder outside of the JAR. Here is what I have tried so far - </p>
  
  <ol>
  <li><p><strong>Maven Compile to a single jar</strong> from <a href="https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven">here</a> , using this approach the single JAR is created with the dependencies included as classes. When running the JAR using -</p>
  
  <p><strong>“java –jar jarName.jar”</strong></p>
  
  <p>I get an error stating - "Error: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace <a href="http://www.springframework.org/schema/context]" rel="noreferrer">http://www.springframework.org/schema/context]</a>"</p></li>
  <li><p><strong>Using Export ‘As Runnable JAR file’</strong> from Eclipse, and choose <strong>‘Extract required libraries into generated JAR’</strong> in the Library Handling section.</p>
  
  <p>This also builds one jar with the dependencies as classes within it. When running the jar, I get the same “Error: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [<a href="http://www.springframework.org/schema/context]" rel="noreferrer">http://www.springframework.org/schema/context]</a>”</p></li>
  <li><p><strong>Using Export ‘As Runnable JAR file’</strong> from Eclipse, and choose <strong>‘Package required libraries into generated JAR’</strong> in the Library Handling section. </p>
  
  <p>This builds one jar with the dependencies inside it as JARs (not classes). When I run this JAR I get the following error –</p>
  
  <p>“Cannot search for matching files underneath URL [rsrc:com/company/] because it does not correspond to a directory in the file system java.io.FileNotFoundException: URL [rsrc:com/company/] cannot be resolved to absolute file path because it does not reside in the file system: rsrc:com/company/  at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:210”</p>
  
  <p>So the JAR ran but was unable to do a scan for the components I was asking it to find. </p></li>
  <li><p><strong>Using Export ‘As Runnable JAR file’</strong> from Eclipse, and choose <strong>‘Copy required libraries into a sub-folder next to the generated JAR’</strong> in the Library Handling section. </p>
  
  <p>This creates a small JAR with a folder next to it which contains all of the dependencies as JARs. When I run this, everything works!</p></li>
  </ol>
  
  <p>Therefore I don’t think it’s an issue with my code, there seems to be an issue with packaging spring that does a scan inside a single JAR. Would that be correct?</p>
  
  <p>Does anyone have any advice on how to build a spring based application doing a package/component scan into a single runnable JAR?</p>
  
  <p><strong>ANSWER</strong>:</p>
  
  <p>I added the following XML to my POM file and just used "mvn package", it created a single executable jar that worked. Why it worked is still a mystery. </p>
  
  <pre><code>      &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
              &lt;version&gt;2.3&lt;/version&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;phase&gt;package&lt;/phase&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;shade&lt;/goal&gt;
                      &lt;/goals&gt;
                      &lt;configuration&gt;
                          &lt;transformers&gt;
                              &lt;transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"&gt;
                                  &lt;mainClass&gt;com.company.project.MainApp&lt;/mainClass&gt;
                              &lt;/transformer&gt;
                              &lt;transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"&gt;
                                  &lt;resource&gt;META-INF/spring.handlers&lt;/resource&gt;
                              &lt;/transformer&gt;
                              &lt;transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"&gt;
                                  &lt;resource&gt;META-INF/spring.schemas&lt;/resource&gt;
                              &lt;/transformer&gt;
                          &lt;/transformers&gt;
                      &lt;/configuration&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
          &lt;/plugin&gt;
  </code></pre>