# java.lang.ExceptionInInitializerError: Wieso???



## Sascha Kunitz (8. Apr 2014)

Ich beschäftige mich gerad zum erstnmal mit JSF. Als erstes kleines Projekt will ich mir ein Login erstellen. Hierzu habe ich mir mit NetBeans 7.4 mit Glassfisch 4.0 folgendes Projekt erstellt.

WebPages/index.xhtml

```
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>JSF Hibernate CRUD Example</title>
        <style>
            tfoot{
                text-align: center;
            }
            thead{
                background-color: magenta;
                color: white;
            }
            td{
                font-weight: bold;
            }
            .msg{
                font-size: small;
                color: blue;
            }
        </style>
    </h:head>
    <h:body>
        <h:form>
            <h:panelGrid id="panel1" columns="2" border="1"
                         cellpadding="5" cellspacing="1">
                <f:facet name="header">
                    <h:outputText value="Add Customer Information"/>
                </f:facet>
                <h:outputLabel value="First Namer:"/>
                <h:inputText value="#{customer.firstName}" id="fn"/>
                <h:outputLabel value="Last Name:"/>
                <h:inputText value="#{customer.lastName}" id="ln"/>
                <h:outputLabel value="Email:"/>
                <h:inputText value="#{customer.email}" id="eml"/>
                <h:outputLabel value="Date of Birth:"/>
                <h:inputText value="#{customer.sd}" id="s"/>
                <f:facet name="footer">
                    <h:outputLabel value="#{customer.msg}" id="msg" styleClass="msg"/>
                    <h:commandButton value="Save" action="#{customer.saveCustomer}">
                    <f:ajax render="fn ln eml s msg" execute="@form"/>
                        </h:commandButton>
                </f:facet>
            </h:panelGrid>
             
        </h:form>
         
        <h:form>
                <h:panelGrid id="panel2" columns="2" border="1"
                             cellpadding="5" cellspacing="1">
                    <f:facet name="header">
                        <h:outputText value="Update/Delete Customer Info"/>
                    </f:facet>
                    <h:outputLabel value="Select Customer:"/>
                    <h:selectOneMenu value="#{customer.selectedname}" id="ulist">
                        <f:selectItems value="#{customer.allCustomers}"/>
                        <f:ajax event="change" render="cid fname lname email sd" listener="#{customer.fullInfo}"/>
                    </h:selectOneMenu>
                      <h:outputLabel value="Customer ID:"/>
                      <h:inputText value="#{customer.custId}" id="cid" readonly="true"/>
                    <h:outputLabel value="First Name:"/>
                    <h:inputText value="#{customer.firstName}" id="fname"/>
                    <h:outputLabel value="Last Name:"/>
                    <h:inputText value="#{customer.lastName}" id="lname"/>
                    <h:outputLabel value="Email:"/>
                    <h:inputText value="#{customer.email}" id="email"/>
                    <h:outputLabel value="Date of Birth:"/>
                    <h:inputText value="#{customer.sd}" id="sd"/>
                    <f:facet name="footer">
                        <h:outputLabel value="#{customer.msg}" id="msg2" styleClass="msg"/>
                        <h:commandButton value="Update Info" action="#{customer.updateCustomer}">
                            <f:ajax render="ulist cid fname lname email sd msg2" execute="@form"/>
                        </h:commandButton>
                        <h:commandButton value="Delete Info" action="#{customer.deleteCustomer}">
                            <f:ajax render="ulist cid fname lname email sd msg2" execute="@form"/>
                        </h:commandButton>
                    </f:facet>
                </h:panelGrid>
            </h:form>
    </h:body>
</html>
```


```
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsflogin.dao;

import com.jsflogin.entity.Customer;
import com.jsflogin.utils.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
 
/**
 *
 * @author javaknowledge
 */
public class CustomerDao {
 
    public void addCustomer(Customer cust) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            session.save(cust);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public void deleteCustomer(int custid) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            Customer cust = (Customer) session.load(Customer.class, new Integer(custid));
            session.delete(cust);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public void updateCustomer(Customer cust) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            session.update(cust);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public List<Customer> getAllCustomers() {
        List<Customer> users = new ArrayList<Customer>();
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            users = session.createQuery("select concat(first_name, ' ', last_name) as name from Customer").list();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return users;
    }
 
    public List<Customer> getCustomerById(String custid) {
        System.out.println(custid);
//        Customer cust = null;
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            String queryString = "from Customer where concat(first_name, ' ', last_name) = :id";
            Query query = session.createQuery(queryString);
            query.setString("id", custid);
            //cust = (Customer) query.uniqueResult();
            List<Customer> list = query.list();
            if (list.size() > 0) {
                return list;
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
}
```


```
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsflogin.dao;

import com.jsflogin.entity.Customer;
import com.jsflogin.utils.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
 
/**
 *
 * @author javaknowledge
 */
public class CustomerDao {
 
    public void addCustomer(Customer cust) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            session.save(cust);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public void deleteCustomer(int custid) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            Customer cust = (Customer) session.load(Customer.class, new Integer(custid));
            session.delete(cust);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public void updateCustomer(Customer cust) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            session.update(cust);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public List<Customer> getAllCustomers() {
        List<Customer> users = new ArrayList<Customer>();
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            users = session.createQuery("select concat(first_name, ' ', last_name) as name from Customer").list();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return users;
    }
 
    public List<Customer> getCustomerById(String custid) {
        System.out.println(custid);
//        Customer cust = null;
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            String queryString = "from Customer where concat(first_name, ' ', last_name) = :id";
            Query query = session.createQuery(queryString);
            query.setString("id", custid);
            //cust = (Customer) query.uniqueResult();
            List<Customer> list = query.list();
            if (list.size() > 0) {
                return list;
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
}
```


```
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jsflogin.utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
  
public class HibernateUtil {
  
    private static final SessionFactory sessionFactory=buildSessionFactory();
  
   public static SessionFactory buildSessionFactory(){
        try {
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
  
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
```

src/com/jsflogin/entity/Customer.hbm.xml
[XML]
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 5, 2013 10:57:32 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="com.javaknowledge.entity.Customer" table="customer" catalog="jsfhibercrud">
        <id name="custId" type="java.lang.Integer">
            <column name="cust_id" />
            <generator class="identity" />
        </id>
        <property name="firstName" type="string">
            <column name="first_name" length="45" not-null="true" />
        </property>
        <property name="lastName" type="string">
            <column name="last_name" length="45" not-null="true" />
        </property>
        <property name="email" type="string">
            <column name="email" length="45" not-null="true" />
        </property>
        <property name="dob" type="date">
            <column name="dob" length="10" not-null="true" />
        </property>
    </class>
</hibernate-mapping>
[/XML]

lt. Netbeans ist die Verbindung zur Datenbank für Hibernate korrekt.
Beim starten erhalte ich aber 


```
java.lang.ExceptionInInitializerError
```

und ich weis damit noch nichts anzufangen. Kann mir bitte einer hier weiterhelfen?


----------



## tandraschko (8. Apr 2014)

Ohne Stacktrace wirds schwer :rtfm:


----------



## Sascha Kunitz (8. Apr 2014)

Hier mal der Stack Trace

```
java.lang.ExceptionInInitializerError
	at com.jsflogin.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
	at com.jsflogin.utils.HibernateUtil.<clinit>(HibernateUtil.java:14)
	at com.jsflogin.dao.CustomerDao.getAllCustomers(CustomerDao.java:80)
	at com.jsflogin.entity.Customer.getAllCustomers(Customer.java:142)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	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 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.UISelectItems.getValue(UISelectItems.java:129)
	at com.sun.faces.renderkit.SelectItemsIterator.initializeItems(SelectItemsIterator.java:208)
	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:924)
	at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
	at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
	at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
	at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
	at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
	at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
	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:443)
	at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
	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:647)
	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:357)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
	at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
	at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
	at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
	at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
	at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
	at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
	at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
	at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
	at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
	at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
	at java.lang.Thread.run(Thread.java:722)
Caused by: org.hibernate.HibernateException: Unable to get the default Bean Validation factory
	at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:127)
	at org.hibernate.cfg.Configuration.applyBeanValidationConstraintsOnDDL(Configuration.java:1704)
	at org.hibernate.cfg.Configuration.applyConstraintsToDDL(Configuration.java:1654)
	at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1445)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
	at com.jsflogin.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
	... 66 more
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:118)
	... 71 more
Caused by: org.hibernate.AssertionFailure: Entity class not found
	at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:124)
	... 76 more
Caused by: java.lang.ClassNotFoundException: com.javaknowledge.entity.Customer
	at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1761)
	at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:266)
	at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:170)
	at org.hibernate.cfg.beanvalidation.TypeSafeActivator.applyDDL(TypeSafeActivator.java:121)
	... 76 more
```


----------



## tandraschko (9. Apr 2014)

Dann muss man doch nur mehr die Exception lesen 

Caused by: java.lang.ClassNotFoundException: com.javaknowledge.entity.Customer

Entweder das wird nicht mitdeployed, das package stimmt nicht oder du hast ein ClassLoader problem.


----------

