title: Spring: java.lang.NoClassDefFoundError: Could not initialize class
body: <p>I am developing a little web application in Spring MVC. I get the exception every time I try to get my custom class in any controller, in case if this class uses another custom class. It is easier to show on example:</p>
  
  <p>Controller, where I'm trying to get an object of custom class WireTest:</p>
  
  <pre><code>@Controller
  @RequestMapping("/offices")
  public class OfficesController {
  
    @Autowired
    private WireTest wt;
  
    @RequestMapping("")
    public String offices(Model model) {
      model.addAttribute("test", wt.getString());
      return "offices";
    }
  }
  </code></pre>
  
  <p>The problem happens always, whether I'm creating an object directly or use @Autowired. In code here, I'm showing the case of @Autowired, but that doesn't matter - I could write <code>private WireTest wt = new WireTest()</code>, and the exception would be the same.</p>
  
  <p><code>WireTest.java</code> class:</p>
  
  <pre><code>@Service
  public class WireTest {
    public String getString() {return (DBhelper.getString());}
  }
  </code></pre>
  
  <p>Part of <code>DBhelper.java</code> class (There are also other static members, the full code is below):</p>
  
  <pre><code>public class DBhelper {
      public static String getString() {return "Hi!";}
  }
  </code></pre>
  
  <p>And the exception:</p>
  
  <pre><code>HTTP Status 500 - Handler processing failed; nested exception is         
  java.lang.NoClassDefFoundError: Could not initialize class org.sher.wtpractice.dao.DBhelper
  </code></pre>
  
  <p>I also can use these classes without any problems in console application, so it works outside Spring.</p>
  
  <p>My Spring configuration:</p>
  
  <p><code>web.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
      &lt;!-- Processes application requests --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/dispatcher-servlet.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;
              org.springframework.web.context.ContextLoaderListener
          &lt;/listener-class&gt;
      &lt;/listener&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><code>dispatcher-servlet.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc.xsd"&gt;
  
  &lt;context:component-scan base-package="org.sher.wtpractice.spring, org.sher.wtpractice.dao" /&gt;
      &lt;mvc:annotation-driven /&gt;
      &lt;mvc:resources mapping="/css/**" location="/css/"/&gt;
      &lt;mvc:resources mapping="/js/**" location="/js/"/&gt;
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/jsp/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>I'm using Maven for building and Tomcat 7 as a server, if that matters.
  Spring version is 4.0.1.RELEASE</p>
  
  <p>Update: Complete code of DBhelper.java</p>
  
  <pre><code>import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.Transaction;
  import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.service.ServiceRegistry;
  
  import java.util.Calendar;
  import java.util.Date;
  public class DBhelper {
      private static final SessionFactory sessionFactory = createSessionFactory();
  
      private static SessionFactory createSessionFactory() {
          Configuration configuration = new Configuration();
          configuration.configure();
          ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                  configuration.getProperties()).build();
          return configuration.buildSessionFactory(serviceRegistry);
      }
  
      public static Object queryWrapper(DBoperation op) {
          Session session = sessionFactory.openSession();
          Transaction transaction = null;
          Object result = null;
          try {
              transaction = session.beginTransaction();
              result = op.operation(session);
              session.getTransaction().commit();
          } catch (Exception e) {
              if (transaction != null) {
                  transaction.rollback();
              }
  //            throw e;
          } finally {
              if (session != null &amp;&amp; session.isOpen())
                  session.close();
          }
          return result;
      }
      public static Date normalizeCal(Calendar cal) {
          cal.set(Calendar.MILLISECOND, 0);
          return cal.getTime();
      }
      public static String getString() {return "Hi!";}
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated! </p>