title: Cant get HibernateUtil work
body: <p>I am getting this error when I run my project and can't figure what's going on.</p>
  
  <blockquote>
    <p>java.lang.NoClassDefFoundError: Could not initialize class
    com.sepa.util.HibernateUtil   at
    com.sepa.controller.InicioBean.getProvincias(InicioBean.java:27)  at
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)     at
    javax.el.BeanELResolver.getValue(BeanELResolver.java:363)     at
    com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
      at
    com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
      at com.sun.el.parser.AstValue.getValue(AstValue.java:140)   at
    com.sun.el.parser.AstValue.getValue(AstValue.java:204)    at
    com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
      at
    org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
      at
    com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
      at
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
      at
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
      at
    javax.faces.component.UISelectItem.getItemValue(UISelectItem.java:208)
      at
    com.sun.faces.renderkit.SelectItemsIterator.initializeItems(SelectItemsIterator.java:197)
      at
    com.sun.faces.renderkit.SelectItemsIterator.hasNext(SelectItemsIterator.java:135)
      at
    com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:762)
      at
    com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:847)
      at
    com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:297)
      at
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
      at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)    at
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:890)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
      at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
      at
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:458)
      at
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134)
      at
    javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
      at
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)    at
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)   at
    org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      at
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
      at
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
      at
    org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
      at
    org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)   at
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
      at
    org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
      at
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
      at
    com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
      at
    com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
      at
    org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
      at
    org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
      at
    org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
      at
    org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
      at
    org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
      at
    org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
      at
    org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
      at
    org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
      at
    org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
      at
    org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
      at
    org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
      at
    org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
      at
    org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
      at
    org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
      at
    org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
      at
    org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
      at java.lang.Thread.run(Thread.java:745)</p>
  </blockquote>
  
  <p>This is my InicioBean class</p>
  
  <pre><code>package com.sepa.controller;
  
  import com.sepa.model.Provincia;
  
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.RequestScoped;
  
  import java.util.List;
  
  import com.sepa.util.HibernateUtil;
  
  import org.hibernate.Session;
  import org.hibernate.Query;
  
  @ManagedBean(name = "inicioBean", eager = true)
  @RequestScoped
  public class InicioBean {
  
      private Provincia provincia;
      List&lt;Provincia&gt; provincias;
      String algo;
  
      public List&lt;Provincia&gt; getProvincias() {
          Session session = null;
          provincias = null;
          try {
              session = HibernateUtil.getSessionFactory().openSession();
              Query query = session.createQuery("from Provincia");
              provincias = (List&lt;Provincia&gt;) query.list();
          } catch (Exception e) {
              System.out.println(e.getMessage());
          } finally {
              if (session != null) {
                  session.close();
              }
          }
          return provincias;
      }
  }
  </code></pre>
  
  <p>This is my HibernateUtil class</p>
  
  <pre><code>package com.sepa.util;
  
  import org.hibernate.cfg.Configuration;
  import org.hibernate.SessionFactory;
  
  public class HibernateUtil {
  
      private static final SessionFactory sessionFactory;
  
      static {
          try {
              // Create the SessionFactory from standard (hibernate.cfg.xml)
              // config file.
              sessionFactory = new Configuration().configure().buildSessionFactory();
          } catch (Throwable ex) {
              // Log the exception.
              System.err.println("Initial SessionFactory creation failed." + ex);
              throw new ExceptionInInitializerError(ex);
          }
      }
  
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  }
  </code></pre>
  
  <p>This is my hibernate.cfg.xml</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.MySQL5Dialect&lt;/property&gt;
          &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/SEPA&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
          &lt;property name="hibernate.connection.password"&gt;sepaweb&lt;/property&gt;
          &lt;mapping class="com.sepa.model.Provincia"/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>And this is my pom</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &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/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.sepa&lt;/groupId&gt;
      &lt;artifactId&gt;SEPAWeb&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;name&gt;SEPAWeb&lt;/name&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish&lt;/groupId&gt;
              &lt;artifactId&gt;javax.faces&lt;/artifactId&gt;
              &lt;version&gt;2.2.7&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax&lt;/groupId&gt;
              &lt;artifactId&gt;javaee-web-api&lt;/artifactId&gt;
              &lt;version&gt;7.0&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;5.1.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;5.1.0.Final&lt;/version&gt;
          &lt;/dependency&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.35&lt;/version&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  
      &lt;build&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.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.7&lt;/source&gt;
                      &lt;target&gt;1.7&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>I really don't know what's going on, I tried a lot of things, changed hibernate.cfg.xml's folder, tryed many HibernateUtil files but I can't get this work.</p>
  
  <p><a href="http://i.stack.imgur.com/C5Mmy.png" rel="nofollow">Project's structure</a></p>
  
  <p>Thank!</p>