title: Keep getting java.lang.NoClassDefFoundError in Spring
body: <p>I really do not know what is wrong with my Spring configuration. Could you give me hand?</p>
  
  <p>My code (simplified version):</p>
  
  <p>App.java:</p>
  
  <pre><code>public class App {
  public static void main(String[] args) {
      AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
      ctx.close();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Config.java:</p>
  
  <pre><code>@Configuration
  @ComponentScan
  public class Config {
      @Bean
      public JdbcOperations jdbcTemplate(DataSource ds) {
          return new JdbcTemplate(ds);
      }
  }
  </code></pre>
  
  <p>pom file:</p>
  
  <pre><code> &lt;properties&gt;
  
      &lt;!-- Generic properties --&gt;
      &lt;java.version&gt;1.6&lt;/java.version&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
  
      &lt;!-- Spring --&gt;
      &lt;spring-framework.version&gt;4.2.5.RELEASE&lt;/spring-framework.version&gt;
  
      &lt;!-- Hibernate / JPA --&gt;
      &lt;hibernate.version&gt;4.2.1.Final&lt;/hibernate.version&gt;
  
      &lt;!-- Logging --&gt;
      &lt;logback.version&gt;1.0.13&lt;/logback.version&gt;
      &lt;slf4j.version&gt;1.7.5&lt;/slf4j.version&gt;
  
      &lt;!-- Test --&gt;
      &lt;junit.version&gt;4.11&lt;/junit.version&gt;
  
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;!-- Spring and Transactions --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;${spring-framework.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
          &lt;version&gt;${spring-framework.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Logging with SLF4J &amp; LogBack --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
          &lt;version&gt;${slf4j.version}&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
          &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
          &lt;version&gt;${logback.version}&lt;/version&gt;
          &lt;scope&gt;runtime&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Hibernate --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;version&gt;${hibernate.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
  
      &lt;!-- Test Artifacts --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
          &lt;version&gt;${spring-framework.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;${junit.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
          &lt;version&gt;4.2.5.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.h2database&lt;/groupId&gt;
          &lt;artifactId&gt;h2&lt;/artifactId&gt;
          &lt;version&gt;1.4.185&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;!-- Build an executable JAR --&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
              &lt;version&gt;2.4&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;archive&gt;
                      &lt;manifest&gt;
                          &lt;mainClass&gt;App&lt;/mainClass&gt;
                      &lt;/manifest&gt;
                  &lt;/archive&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  </code></pre>
  
  <p>My folder structure:<br/>
  <a href="https://i.stack.imgur.com/yIGc4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yIGc4.png" alt=""></a></p>
  
  <p>After I maven package it, when I run the jar I got:</p>
  
  <pre><code>    Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/annotation/AnnotationConfigApplicationContext
          at App.main(App.java:10)
  Caused by: java.lang.ClassNotFoundException: org.springframework.context.annotation.AnnotationConfigApplicationContext
          at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
          at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
          at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  </code></pre>
  
  <p>in which, the line 10 is the <code>AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);</code></p>
  
  <p>In addition:
  I am writing the code when I was wathcing a spring tutorial:<a href="https://www.youtube.com/watch?v=diIPBDYK658&amp;t=188s" rel="nofollow noreferrer">Spring Framework Tutorial 4: Database programming with JdbcTemplate</a></p>
  
  <p>And his source code is at:<a href="https://github.com/ubilife/tutorials/tree/master/spring/tutorial4/CustomerJdbc" rel="nofollow noreferrer">github</a>
  The only difference with the source code is that I added the MainClass in the pom file.</p>
  
  <p>Thank you so much!<br/>
  Sincerely</p>