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

Maven+Hibernate项目添加Servlet后出现java.lang.NoClassDefFoundError: org/hibernate/query/Query问题求助

java.lang.NoClassDefFoundError: org/hibernate/query/Query in Maven+Hibernate+Servlet Project (Recurring Issue)

I’ve dealt with similar frustrating classloading issues in Maven web projects before, so let’s break down what’s going on and how to fix it permanently:

What’s Causing This?

You noted that basic Hibernate CRUD worked fine until adding Servlets, and the problem keeps coming back even after temporary fixes. The fact that switching to a Dynamic Web Project fixed it without code changes points straight to a Maven dependency packaging/deployment problem.

The NoClassDefFoundError means the JVM can’t find the org.hibernate.query.Query class at runtime, even though it was available during compilation. For Maven web apps, this almost always happens because required Hibernate JARs aren’t being copied to your deployed app’s WEB-INF/lib directory (where Tomcat looks for runtime dependencies). Dynamic Web Projects usually have JARs manually placed in WEB-INF/lib, so they avoid this issue.

Step-by-Step Fixes

Let’s go through the most reliable solutions in order:

  1. Verify Maven is Packaging Hibernate JARs into the WAR

    • Run mvn clean package in your project directory, then open the generated WAR file (under target/SchoolProject.war) and check if WEB-INF/lib contains hibernate-core-5.6.0.Final.jar and its transitive dependencies. If it’s missing, Maven isn’t including it in the final build.
    • Fix: In your IDE, confirm that "Maven Dependencies" is listed in the Deployment Assembly (Eclipse: Project Properties > Deployment Assembly; IntelliJ: File > Project Structure > Artifacts) and set to deploy to WEB-INF/lib.
  2. Clean Up Duplicate Dependencies

    • Looking at your pom.xml, you’ve included the jstl dependency twice. While this isn’t the direct cause of your Hibernate error, duplicate dependencies can trigger classloading conflicts and unexpected behavior. Remove one of these entries:
      <!-- Remove either this duplicate entry -->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
      </dependency>
      
  3. Check for Tomcat Classloading Conflicts

    • Sometimes Tomcat’s own lib directory has older or conflicting Hibernate JARs that override your project’s dependencies.
    • Fix: Navigate to your Tomcat installation folder > lib, and delete any Hibernate-related JARs (if present). Your project should supply its own Hibernate dependencies.
  4. Refresh Maven Dependencies and Clear Caches

    • Corrupted cached JARs or outdated IDE metadata often make this issue recur.
    • Run mvn clean install -U to force Maven to re-download all dependencies and rebuild the project.
    • In your IDE:
      • Eclipse: Right-click project > Maven > Update Project (check "Force Update of Snapshots/Releases"), then run Project > Clean.
      • IntelliJ: Go to File > Invalidate Caches... > Invalidate and Restart.
  5. Double-Check Dependency Scopes

    • Ensure your hibernate-core dependency uses the default compile scope (which it does in your pom). If it were set to provided, Maven wouldn’t package it into the WAR, and Tomcat doesn’t include Hibernate by default.

Your pom.xml (Cleaned of Duplicates)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse</groupId>
<artifactId>GestionDesAchats1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SchoolProject Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.0.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.2-b02</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
</dependencies>
<build>
<finalName>SchoolProject </finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 06:49:08