java.lang.NoClassDefFoundError:无法初始化类 xxx.xxx.xxx.HibernateUtil

2022-01-21 00:00:00 java Hibernate jersey

在此我添加了 web.xml 的源代码

Herewith I added my source code of web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>ussd</display-name>
    <servlet>
        <servlet-name>init</servlet-name>
        <jsp-file>/init.jsp</jsp-file>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <context-param>
        <param-name>javax.ws.rs.core.Application</param-name>
        <param-value>com.dialog.mife.ussd.api.USSDApplication</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.providers</param-name>
        <param-value>com.dialog.mife.ussd.exception.NotFoundException</param-value>
    </context-param>
    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/</param-value>
    </context-param>
    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>
    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/webresources/*</url-pattern>
    </servlet-mapping>
</web-app>

休眠.cfg.xml.我的 Web 服务项目使用 Jersey+Hibernate..

Hibernate.cfg.xml. My Web service project goes with Jersey+Hibernate..

    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>   
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">xxx</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/USSD</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>   
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.connection.autocommit">true</property>
        <mapping class="com.xxx.xxx.xxx.xxx.Application" />

    </session-factory>
</hibernate-configuration>

这里是 HibernateUtil 类

Here is HibernateUtil Class

public class HibernateUtil {
    private static final SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new ExceptionInInitializerError(ex);
        }
    }

这是我的错误我添加了 Hibernate 最新的 jar 文件.[hibernate-core-4.3.7.Final.jar]

Here is my error I added Hibernate latest jar files. [hibernate-core-4.3.7.Final.jar]

SEVERE: StandardWrapper.Throwable
java.lang.NoClassDefFoundError: Could not initialize class com.dialog.mife.ussd.util.HibernateUtil
    at org.apache.jsp.init_jsp.jspInit(init_jsp.java:23)
    at org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:48)
    at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:180)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.init(JspServlet.java:138)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5123)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5407)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

Nov 11, 2014 2:06:11 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /ussd threw load() exception
java.lang.NoClassDefFoundError: Could not initialize class com.dialog.mife.ussd.util.HibernateUtil
    at org.apache.jsp.init_jsp.jspInit(init_jsp.java:23)
    at org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:48)
    at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:180)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.init(JspServlet.java:138)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5123)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5407)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

请帮我解决这个问题谢谢

Please help me to sort out this issue thanks

推荐答案

SessionFactory初始化失败时会出现这种错误.您提到您正在使用 hibernate-core-4.3.7.您初始化 SessionFactory 的方式不适用于此版本.使用 Hibernate 版本 3.x 或更改初始化 SessionFactory 的方式.下面是在Hibernate 4.x +中构建会话工厂的正确方法.

This Kind of an error can be happened when initialization of SessionFactory fails. You have mentioned that you are using hibernate-core-4.3.7. The way you are initializing the SessionFactory is not the correct for this version. Use Hibernate version 3.x or change the way you are initializing the SessionFactory. Below is the correct way to build the session factory in Hibernate 4.x +.

Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties());
SessionFactory factory = configuration.buildSessionFactory(builder.build());

相关文章