title: NoClassDefFoundError: Could not initialize class ....utils.HibernateSessionFactory
body: <p>help me, please. If I run it on the psvm class Session works perfect. But when it run in tomcat: tomcat display this exception.</p>
  
  <p><strong>Tomcat exception:</strong>  Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class ru.haki.utils.HibernateSessionFactory</p>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>    &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;Haki&lt;/groupId&gt;
    &lt;artifactId&gt;CRUD&lt;/artifactId&gt;
    &lt;packaging&gt;war&lt;/packaging&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;CRUD Maven Webapp&lt;/name&gt;
    &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
    &lt;properties&gt;
      &lt;hibernate-version&gt;5.1.0.Final&lt;/hibernate-version&gt;
      &lt;spring-version&gt;4.2.6.RELEASE&lt;/spring-version&gt;
    &lt;/properties&gt;
  
    &lt;dependencies&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
        &lt;version&gt;${hibernate-version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-commons-annotations&lt;/artifactId&gt;
        &lt;version&gt;3.2.0.Final&lt;/version&gt;
      &lt;/dependency&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;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-osgi&lt;/artifactId&gt;
        &lt;version&gt;${hibernate-version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-envers&lt;/artifactId&gt;
        &lt;version&gt;${hibernate-version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-c3p0&lt;/artifactId&gt;
        &lt;version&gt;${hibernate-version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-proxool&lt;/artifactId&gt;
        &lt;version&gt;${hibernate-version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-infinispan&lt;/artifactId&gt;
        &lt;version&gt;${hibernate-version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-ehcache&lt;/artifactId&gt;
        &lt;version&gt;${hibernate-version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!--driver for connection to MySQL--&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;mysql&lt;/groupId&gt;
        &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
        &lt;version&gt;5.1.34&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.springframework&lt;/groupId&gt;
        &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
        &lt;version&gt;${spring-version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.springframework&lt;/groupId&gt;
        &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
        &lt;version&gt;${spring-version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.springframework&lt;/groupId&gt;
        &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
        &lt;version&gt;${spring-version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;jstl&lt;/groupId&gt;
        &lt;artifactId&gt;jstl&lt;/artifactId&gt;
        &lt;version&gt;1.2&lt;/version&gt;
      &lt;/dependency&gt;
  
  
    &lt;/dependencies&gt;
  
    &lt;build&gt;
      &lt;finalName&gt;CRUD&lt;/finalName&gt;
      &lt;plugins&gt;
  
        &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.5.1&lt;/version&gt;
          &lt;configuration&gt;
            &lt;source&gt;1.8&lt;/source&gt;
            &lt;target&gt;1.8&lt;/target&gt;
            &lt;encoding&gt;UTF-8&lt;/encoding&gt;
          &lt;/configuration&gt;
        &lt;/plugin&gt;
  
        &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.6&lt;/version&gt;
          &lt;configuration&gt;
            &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
          &lt;/configuration&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
    &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>hibernate.cfg.xml</strong></p>
  
  <pre><code>    &lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD//EN"
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
      &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/test&lt;/property&gt;
      &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  
      &lt;property name="connection.username"&gt;root&lt;/property&gt;
      &lt;property name="connection.password"&gt;root&lt;/property&gt;
      &lt;!--&lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;--&gt;
  
      &lt;mapping class="ru.haki.domain.User"/&gt;
  
      &lt;!-- DB schema will be updated if needed --&gt;
      &lt;!-- &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt; --&gt;
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>HibernateSessionFactory</strong></p>
  
  <pre><code>package ru.haki.utils;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.boot.Metadata;
  import org.hibernate.boot.MetadataSources;
  import org.hibernate.boot.registry.StandardServiceRegistry;
  import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  import org.hibernate.cfg.Configuration;
  
  public class HibernateSessionFactory {
  
     private static SessionFactory sessionFactory = buildSessionFactory();
  
      private static SessionFactory buildSessionFactory() {
          final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                  .configure() // configures settings from hibernate.cfg.xml
                  .build();
          try {
              sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
          }
          catch (Exception e) {
              // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
              // so destroy it manually.
              StandardServiceRegistryBuilder.destroy(registry);
          }
          return sessionFactory;
      }
  
      public static Session getSession() {
          return sessionFactory.openSession();
      }
  
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      public static void closeSession(Session session) {
          if (session != null) {
              session.flush();
              session.close();
          }
      }
  }
  </code></pre>
  
  <p><strong>User</strong></p>
  
  <pre><code>package ru.haki.domain;
  
  //import org.hibernate.annotations.CreationTimestamp;
  
  import javax.persistence.*;
  import java.util.Date;
  
  @Entity
  @Table(name = "user", schema = "", catalog = "test")
  public class User {
      private int id;
      private String name;
      private Integer age;
      private boolean isAdmin;
      private Date createdDate;
  
      public User() {
          this.createdDate = new Date();
      }
  
      public User(String name, Integer age, boolean isAdmin) {
          this.name = name;
          this.age = age;
          this.isAdmin = isAdmin;
          this.createdDate = new Date();
      }
  
      @Id
      @Column(name = "id", nullable = false, insertable = true, updatable = true)
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      @Basic
      @Column(name = "name", nullable = false, insertable = true, updatable = true, length = 25)
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @Basic
      @Column(name = "age", nullable = true, insertable = true, updatable = true)
      public Integer getAge() {
          return age;
      }
  
      public void setAge(Integer age) {
          this.age = age;
      }
  
      @Basic
      @Column(name = "isAdmin", nullable = false, insertable = true, updatable = true)
      public boolean isAdmin() {
          return isAdmin;
      }
  
      public void setAdmin(boolean admin) {
          isAdmin = admin;
      }
  
  
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name = "createdDate", nullable = false)
      public java.util.Date getCreatedDate() {
          return createdDate;
      }
  
      public void setCreatedDate(Date createdDate) {
          this.createdDate = createdDate;
      }
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (o == null || getClass() != o.getClass()) return false;
  
          User user = (User) o;
  
          if (id != user.id) return false;
          if (isAdmin != user.isAdmin) return false;
          if (name != null ? !name.equals(user.name) : user.name != null) return false;
          if (age != null ? !age.equals(user.age) : user.age != null) return false;
          if (createdDate != null ? !createdDate.equals(user.createdDate) : user.createdDate != null) return false;
  
          return true;
      }
  
      @Override
      public int hashCode() {
          int result = id;
          result = 31 * result + (name != null ? name.hashCode() : 0);
          result = 31 * result + (age != null ? age.hashCode() : 0);
          result = 31 * result + (isAdmin ? 1 : 0);
          result = 31 * result + (createdDate != null ? createdDate.hashCode() : 0);
          return result;
      }
  
      @Override
      public String toString() {
          return "User{" +
                  "id=" + id +
                  ", name='" + name + '\'' +
                  ", age=" + age +
                  ", isAdmin=" + isAdmin +
                  ", createdDate=" + createdDate +
                  '}';
      }
  }
  </code></pre>