Java NoClassDefFoundError:org/hibernate/service/ServiceRegistry

Java NoClassDefFoundError:org/hibernate/service/ServiceRegistry,java,hibernate,Java,Hibernate,我不明白为什么我在Servlet中遇到这个错误,但在测试类中没有:java.lang.NoClassDefFoundError:org/hibernate/service/ServiceRegistry 我有冬眠5.1.14 下面是servlet。代码在BookDAO=daooutilities.getBookDAO()处崩溃。我测试过,它实际上在我所有的DAO上都崩溃了。它只是获取一个数据访问对象(DAO),这样我就可以检索数据库中的所有书籍。我还将包括工作测试类以进行比较。如果你还需要看别的

我不明白为什么我在Servlet中遇到这个错误,但在测试类中没有:
java.lang.NoClassDefFoundError:org/hibernate/service/ServiceRegistry

我有冬眠5.1.14

下面是servlet。代码在BookDAO=daooutilities.getBookDAO()处崩溃。我测试过,它实际上在我所有的DAO上都崩溃了。它只是获取一个数据访问对象(DAO),这样我就可以检索数据库中的所有书籍。我还将包括工作测试类以进行比较。如果你还需要看别的东西,请告诉我

编辑:我没有使用Maven(至少不是故意的)。我正在手动添加罐子

package examples.pubhub.servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import examples.pubhub.dao.*;
import examples.pubhub.model.*;
import examples.pubhub.utilities.DAOUtilities;

/*
 * This servlet will take you to the homepage for the Book Publishing module (level 100)
 */
@WebServlet("/BookPublishing")
public class BookPublishingServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //Get our DAOs
        System.out.println("Getting DAOs");
        BookDAO dao = DAOUtilities.getBookDAO(); //fails here
        TagDAO tgdao = DAOUtilities.getTagDAO();


        // Grab the list of Books from the Database
        List<Book> bookList = dao.getAllBooks();

        //Grab the tags for each book from the Database
        HashMap<String, String> booktags = new HashMap<String, String>();
        for(Book b : bookList) {
            String tags = "";
            List<Tag> tempTags = tgdao.getTagsByBook(b);
            for(Integer i=0; i<tempTags.size(); i++) {
                tags+=tempTags.get(i).getName()+";";
            }
            tags = tags.substring(0, tags.length() - 1);
            booktags.put(b.getIsbn13(), tags);
        }


        // Populate the list into a variable that will be stored in the session
        request.getSession().setAttribute("books", bookList);
        request.getSession().setAttribute("booktags", booktags);

        request.getRequestDispatcher("bookPublishingHome.jsp").forward(request, response);
    }
}
以下是测试类:

package examples.pubhub.test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import examples.pubhub.dao.*;
import examples.pubhub.model.*;
import examples.pubhub.utilities.DAOUtilities;

public class TestTagDAO {
    //isbn:1111111111111
    public static void main(String[] args) {
        String isbn = "1111111111111";
        String name1 = "test1";
        String name2 = "test2";
        String name3 = "test3";
        String name4 = "test4";
        List<Tag> tags = new ArrayList<>();
        List<Book> books = new ArrayList<Book>();

        TagDAO tagDao = DAOUtilities.getTagDAO();
        BookDAO bookDao = DAOUtilities.getBookDAO();

        Book book = bookDao.getBookByISBN(isbn);
        books = bookDao.getAllBooks();
    }

}
package examples.pubhub.test;
导入java.util.ArrayList;
导入java.util.HashSet;
导入java.util.List;
导入java.util.Set;
导入examples.pubhub.dao.*;
导入examples.pubhub.model.*;
导入examples.pubhub.utilities.DAOUtilities;
公共类TestTagDAO{
//国际标准书号:1111
公共静态void main(字符串[]args){
字符串isbn=“1111111”;
字符串name1=“test1”;
字符串name2=“test2”;
字符串name3=“test3”;
字符串name4=“test4”;
列表标记=新的ArrayList();
List books=new ArrayList();
TagDAO TagDAO=DAOUtilities.getTagDAO();
BookDAO BookDAO=daooutilities.getBookDAO();
Book Book=bookDao.getBookByISBN(isbn);
books=bookDao.getAllBooks();
}
}

您是否已将hibernate核心依赖项添加到pom.xml中

如果没有,请添加如下内容

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.x.x</version>
</dependency>

org.hibernate
冬眠核心
5.x.x

我最近在使用Eclipse时经常遇到这个问题。我发现Eclipse有时会随机地(令人烦恼地)生成所需的.class文件。通常我会从我的项目中删除/bin/目录,然后刷新和清理我的项目,它会生成相应的.class文件。执行此操作之前,请检查.class文件是否在/bin/目录中


此外,如果手动添加.JAR文件,请检查它们是否在构建路径中。

我没有使用maven。我在eclipse中将HibernateJAR添加到构建路径中。
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.x.x</version>
</dependency>