java.lang.NoClassDefFoundError:无法初始化类DataLayer.HibernateAdapter

时间:2015-03-10 09:07:48

标签: java hibernate-annotations hibernate-session

我已将Java项目(Web服务)修改为动态Web模块。我使用Tomcat 7.0.59作为服务器。启动服务器时,它开始没有任何问题。但是,如果我尝试访问Web服务方法,那么我最终会得到错误 - “无法初始化类DataLayer.HibernateAdapter java.lang.NoClassDefFoundError:无法初始化类DataLayer.HibernateAdapter ”。任何人请帮助我,而不仅仅是标记为“重复”。如果必须修改代码,请提供详细步骤。谢谢!

控制台日志:

Mar 10, 2015 2:09:07 PM com.sun.xml.ws.server.sei.EndpointMethodHandler invoke
SEVERE: Could not initialize class DataLayer.HibernateAdapter
java.lang.NoClassDefFoundError: Could not initialize class DataLayer.HibernateAdapter
    at DataLayer.DatabaseContext.<init>(DatabaseContext.java:12)
    at DataLayer.ConsumerDetails.getConsumerdetails(ConsumerDetail.java:84)
    at ManageLayer.Authenticate(AuthenticationManager.java:50)
    at ManageLayer.Console.GetProductsList(Console.java:484)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.xml.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:246)
    at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:146)

DatabaseContext.java:

public class DatabaseContext
{
    private final Session session;

    public DatabaseContext() {
        this.session = HibernateAdapter.getSessionFactory().openSession();
    }

    public Session delegate() {
        return session;
    }

    public void close() {
        session.flush();
        session.close();
    }
}

class HibernateAdapter
   {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try 
        {

            return new AnnotationConfiguration()
            .addAnnotatedClass(Consumer.class)
            .addAnnotatedClass(Product.class)
            .addAnnotatedClass(PriceTag.class)
            .addAnnotatedClass(Barcode.class)
            .configure().buildSessionFactory();
        } 
        catch (Throwable e) 
        {
            System.err.println("Exception while creating Initial SessionFactory" + e);
            throw new ExceptionInInitializerError(e);
        }
    }

    public static SessionFactory getSessionFactory() 
    {
        return sessionFactory;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }
}

1 个答案:

答案 0 :(得分:1)

NoClassDefFoundError通常表示您的班级路径不正确。检查类路径中是否有正确的Hibernate库。例如在项目设置中,如果您使用的是Eclipse。现在,您没有正确地包含DataLayer.HibernateAdapter类,因此Tomcat无法找到它。