简体   繁体   中英

Maven java.lang.NoClassDefFoundError: org/hibernate/query/Query

I'm working on a project and it kept generating error when runing the project.

I need help to solve this issue please, thank you in advance.

This is my first time using Maven and Hibernate and it's giving tons of errors such as

java.lang.NoClassDefFoundError: org/hibernate/query/Query at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1407) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1215)

I have tried this solution it works (the servlet works too) for a little while then back to the same problem again.

pom.xml :

        <?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>GestionDesAchats1 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>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            
            <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>8.0.27</version>
            </dependency>
            
          </dependencies>
        
          <build>
            <finalName>GestionMatiere</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>

jars :

在此处输入图像描述

Error

在此处输入图像描述

Try running an mvn dependency:tree to see what your classpath looks like.

There is a compatibility matrix between hibernate, REST and other components. That means you have to use the right versions together . It's often non-trivial to figure out which ones to combine, but mvn dependency:tree -Dverbose will give you insight where you overwrite versions. These days, most platforms have a website to generate a compatible pom for you ( Quarkus , Spring, etc). But old-school war based deployments typically don't have that yet. Non-war based deployments of Hibernate with Rest look differently: here's an example .

A quick search shows that the class org.hibernate.query.Query existed in hibernate-core 5.2, but not in 5.6, where it got moved to org.hibernate:hibernate-search-orm.

Try adding a dependency on hibernate-search-orm too:

        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-search-orm</artifactId>
          <version>5.6.0.Final</version>
        </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM