You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Hibernate的Web应用出现ClassNotFoundException问题求助

问题分析与解决方案

Hey there! Let's tackle your Hibernate Web app issues head-on. The primary error you're hitting is java.lang.ClassNotFoundException: javax.xml.bind.JAXBException, and we'll also fix the file path confusion that might be contributing to your FileNotFoundException.

1. Fix the javax.xml.bind.JAXBException Class Not Found Error

Starting with the critical exception: JAXB API was removed from the default classpath in Java 9 and higher (it was part of Java EE modules that were deprecated and removed). Hibernate relies on JAXB for XML processing, so you need to add it explicitly to your project.

If you're using Maven:

Add these dependencies to your pom.xml:

<dependencies>
    <!-- JAXB API -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <!-- JAXB Implementation -->
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.1</version>
        <scope>runtime</scope>
    </dependency>
    <!-- JAXB Activation Framework -->
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
</dependencies>

If you're using a non-Maven project:

Download these JARs and add them to your WEB-INF/lib folder:

  • jaxb-api-2.3.1.jar
  • jaxb-impl-2.3.1.jar
  • activation-1.1.1.jar

2. Fix the File Path Issues in Hibernate Configuration

You mentioned a FileNotFoundException despite specifying paths—this is because you're using absolute filesystem paths instead of classpath paths. The src/main/resources directory is automatically added to your project's classpath, so you don't need to include it in your paths.

Update your addEmployee method:

Change the configure line from:

Configuration configuration = new Configuration().configure("src/main/resources/hibernate.cfg.xml");

To:

Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
// Or even simpler, let Hibernate auto-detect it:
// Configuration configuration = new Configuration().configure();

Update your Hibernate config file (hibernate.cfg.xml):

Change the mapping resource line from:

<mapping resource="src/main/resources/Employee-hbn.xml" />

To:

<mapping resource="Employee-hbn.xml" />

3. Bonus: Improve Your Hibernate SessionFactory Setup

While we're at it, the older Configuration.buildSessionFactory() method is deprecated in newer Hibernate versions. Here's the recommended way to build your SessionFactory (for Hibernate 5+):

try {
    StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
            .configure("hibernate.cfg.xml") // Uses classpath path
            .build();
    SessionFactory sessionFactory = new MetadataSources(registry)
            .buildMetadata()
            .buildSessionFactory();
    Session session = sessionFactory.openSession();
    // Rest of your code...
} catch (HibernateException e) {
    e.printStackTrace(); // Better to print full stack trace for debugging
    System.out.println("Error adding employee: " + e.getMessage());
}

Your Original Error Log (Formatted)

java.lang.ClassNotFoundException: javax.xml.bind.JAXBException 
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275) 
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104) 
at org.hibernate.boot.spi.XmlMappingBinderAccess.<init>(XmlMappingBinderAccess.java:43) 
at org.hibernate.boot.MetadataSources.<init>(MetadataSources.java:86) 
at org.hibernate.cfg.Configuration.<init>(Configuration.java:123) 
at org.hibernate.cfg.Configuration.<init>(Configuration.java:118) 
at eql.com.dao.EmployeeDAO.addEmployee(EmployeeDAO.java:16) 
at eql.com.controller.EmployeeControllerServlet.doPost(EmployeeControllerServlet.java:27) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) 
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) 
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407) 
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754) 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376) 
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
at java.base/java.lang.Thread.run(Thread.java:844) 

Your Original addEmployee Method (Formatted)

public void addEmployee(String FirstName,String LastName,String email,String Password,String phone, String City) { 
    try { 
        Configuration configuration = new Configuration().configure("src/main/resources/hibernate.cfg.xml"); 
        SessionFactory sessionFactory=configuration.buildSessionFactory(); 
        Session session=sessionFactory.openSession(); 
        Transaction transaction =session.beginTransaction(); 
        Employee em=new Employee(); 
        em.setFirstname(FirstName); 
        em.setLastname(LastName); 
        em.setEmailid(email); 
        em.setPassword(Password); 
        em.setMobile(phone); 
        em.setCity(City); 
        session.save(em); 
        transaction.commit(); 
        System.out.println("\n\n Details Added"); 
    } catch(HibernateException e) { 
        System.out.println(e.getMessage()); 
        System.out.println("error"); 
    } 
}

Your Original Hibernate Configuration File (Formatted)

<hibernate-configuration> 
    <session-factory> 
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> 
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/testdb</property> 
        <property name="hibernate.connection.username">postgres</property> 
        <property name="hibernate.connection.password">sql123</property> 
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> 
        <property name="show_sql">true</property> 
        <property name="format_sql">true</property> 
        <property name="hbm2ddl.auto">create </property> 
        <mapping resource="src/main/resources/Employee-hbn.xml" /> 
    </session-factory> 
</hibernate-configuration>

内容的提问来源于stack exchange,提问作者Jyoti Jadhav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:27:23