title: How do I put all required JAR files in a library folder inside the final JAR file with Maven?
body: <p>I am using Maven in my standalone application, and I want to package all the dependencies in my JAR file inside a library folder, as mentioned in one of the answers here:</p>
  
  <p><em><a href="https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven">How can I create an executable JAR with dependencies using Maven?</a></em></p>
  
  <p>I want my final JAR file to have a library folder that contains the dependencies as JAR files, not like what the <code>maven-shade-plugin</code> that puts the dependencies in the form of folders like the Maven hierarchy in the .m2 folder.</p>
  
  <p>Well, actually the current configuration does what I want, but I am having a problem with loading the JAR files when running the application. I can't load the classes.</p>
  
  <p>Here's my configuration:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;plugins&gt;
  
      &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
          &lt;executions&gt;
              &lt;execution&gt;
                  &lt;id&gt;copy-dependencies&lt;/id&gt;
                  &lt;phase&gt;prepare-package&lt;/phase&gt;
                  &lt;goals&gt;
                      &lt;goal&gt;copy-dependencies&lt;/goal&gt;
                  &lt;/goals&gt;
                  &lt;configuration&gt;
                      &lt;outputDirectory&gt;${project.build.directory}/classes/lib&lt;/outputDirectory&gt;
                      &lt;overWriteReleases&gt;false&lt;/overWriteReleases&gt;
                      &lt;overWriteSnapshots&gt;false&lt;/overWriteSnapshots&gt;
                      &lt;overWriteIfNewer&gt;true&lt;/overWriteIfNewer&gt;
                  &lt;/configuration&gt;
              &lt;/execution&gt;
          &lt;/executions&gt;
      &lt;/plugin&gt;
  
      &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
          &lt;configuration&gt;
              &lt;archive&gt;
                  &lt;manifest&gt;
                      &lt;addClasspath&gt;true&lt;/addClasspath&gt;
                      &lt;classpathPrefix&gt;lib/&lt;/classpathPrefix&gt;
                      &lt;mainClass&gt;com.myapp.MainClass&lt;/mainClass&gt;
                  &lt;/manifest&gt;
              &lt;/archive&gt;
          &lt;/configuration&gt;
      &lt;/plugin&gt;
  
      &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
          &lt;configuration&gt;
              &lt;source&gt;1.6&lt;/source&gt;
              &lt;target&gt;1.6&lt;/target&gt;
          &lt;/configuration&gt;
      &lt;/plugin&gt;
  
      &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
          &lt;executions&gt;
              &lt;execution&gt;
                  &lt;id&gt;install&lt;/id&gt;
                  &lt;phase&gt;install&lt;/phase&gt;
                  &lt;goals&gt;
                      &lt;goal&gt;sources&lt;/goal&gt;
                  &lt;/goals&gt;
              &lt;/execution&gt;
          &lt;/executions&gt;
      &lt;/plugin&gt;
  
      &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.5&lt;/version&gt;
          &lt;configuration&gt;
              &lt;encoding&gt;UTF-8&lt;/encoding&gt;
          &lt;/configuration&gt;
      &lt;/plugin&gt;
  
  &lt;/plugins&gt;
  </code></pre>
  
  <p>The project runs fine from Eclipse, and the JAR files are put in the library folder inside my final JAR file as I want, but when running the final JAR file from the target folder I always get <code>ClassNotFoundException</code>:</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
  Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
          at java.net.URLClassLoader$1.run(Unknown Source)
          at java.security.AccessController.doPrivileged(Native Method)
          at java.net.URLClassLoader.findClass(Unknown Source)
          at java.lang.ClassLoader.loadClass(Unknown Source)
          at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
          at java.lang.ClassLoader.loadClass(Unknown Source)
  Could not find the main class: com.myapp.MainClass. Program will exit.
  </code></pre>
  
  <p>How can I fix this exception?</p>