java.lang.NoClassDefFoundError: Could not initialize class DataLayer.HibernateAdapter

java.lang.NoClassDefFoundError: Could not initialize class DataLayer.HibernateAdapter

我已经将我的 Java 项目(网络服务)修改为动态网络模块。我使用 Tomcat 7.0.59 作为服务器。启动服务器时,它可以毫无问题地启动。但是,如果我尝试访问 Web 服务方法,那么我将以错误告终 -"Could not initialize class DataLayer.HibernateAdapter java.lang.NoClassDefFoundError: Could not initialize classDataLayer.HibernateAdapter”。任何人都请帮助我,而不仅仅是将其标记为 "Duplicate"。如果必须修改代码,请提供详细步骤。谢谢!!

控制台日志:

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.invoke(InstanceResolver.java:246)
    at com.sun.xml.ws.server.InvokerTube.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();
    }
}

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