错误java.lang.NoClassDefFoundError:com/mongodb/MongoClient

错误java.lang.NoClassDefFoundError:com/mongodb/MongoClient,java,mongodb,maven,tomcat,runtime-error,Java,Mongodb,Maven,Tomcat,Runtime Error,所以我收到了一条非常奇怪的错误信息。我目前正在与maven一起开发一个JavaWeb项目,并用Eclipse和Tomcat测试该项目。所以我导入了所有必要的依赖项(MongoJava驱动程序、mongodb驱动程序、mongodb驱动程序核心、bson和javax.servlet api),或者我是这么认为的。但我还是一次又一次地犯这个错误 如果我把代码作为main方法的一部分运行,它就可以正常工作……所以我不知道是什么导致了这个问题 这是我的MongoDB连接器 public class Co

所以我收到了一条非常奇怪的错误信息。我目前正在与maven一起开发一个JavaWeb项目,并用Eclipse和Tomcat测试该项目。所以我导入了所有必要的依赖项(MongoJava驱动程序、mongodb驱动程序、mongodb驱动程序核心、bson和javax.servlet api),或者我是这么认为的。但我还是一次又一次地犯这个错误

如果我把代码作为main方法的一部分运行,它就可以正常工作……所以我不知道是什么导致了这个问题

这是我的MongoDB连接器

public class Connector {

    final String HOST = "localhost";
    final int PORT = 27017;
    final String DBNAME = "mitfahrapp";
    public static Connector instance;
    public MongoClient connection;
    public MongoDatabase database;



    public Connector(){
        this.connection = new MongoClient(this.HOST, this.PORT);
        this.database = connection.getDatabase(DBNAME);

    }

    public MongoClient getClient() {
        return connection;
    }


    public static Connector createInstance() throws UnknownHostException {

        if (Connector.instance == null) {
            Connector.instance = new Connector();
        }
        return Connector.instance;
    }

    public MongoCollection<Document> getCollection(String name) {
        return this.database.getCollection(name);
    }

    public void CloseMongo() {
        connection.close();
    }
}

公共类连接器{
最后一个字符串HOST=“localhost”;
最终int端口=27017;
最后一个字符串DBNAME=“mitfahrapp”;
公共静态连接器实例;
公共MongoClient连接;
公共蒙哥达数据库;
公共连接器(){
this.connection=新的MongoClient(this.HOST,this.PORT);
this.database=connection.getDatabase(DBNAME);
}
公共MongoClient getClient(){
回路连接;
}
公共静态连接器createInstance()引发UnknownHostException{
if(Connector.instance==null){
Connector.instance=新连接器();
}
返回Connector.instance;
}
公共MongoCollection getCollection(字符串名称){
返回此.database.getCollection(名称);
}
公共空间关闭Mongo(){
connection.close();
}
}
这是我的LoginServlet.java的一部分

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


            Connector c = Connector.createInstance();
            MongoCollection<Document> collection = c.getCollection("users");

            String username = request.getParameter("username");
            String password = request.getParameter("password");

            Bson filterUsername = Filters.eq("username", username);
            Bson filterPwd = Filters.eq("password", password);
            Bson bsonFilter = Filters.and(filterUsername, filterPwd);

            FindIterable<Document> doc = collection.find(bsonFilter);
            if (doc != null) {
                response.sendRedirect("welcome.jsp");

            } else {
                response.sendRedirect("login.jsp");
            }
protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)
抛出ServletException、IOException{
连接器c=Connector.createInstance();
MongoCollection collection=c.getCollection(“用户”);
字符串username=request.getParameter(“用户名”);
字符串密码=request.getParameter(“密码”);
Bson filterUsername=Filters.eq(“用户名”,用户名);
Bson filterPwd=Filters.eq(“密码”,password);
Bson bsonFilter=Filters.and(filterUsername,filterPwd);
FindTable文档=collection.find(bsonFilter);
如果(doc!=null){
sendRedirect(“welcome.jsp”);
}否则{
sendRedirect(“login.jsp”);
}

感谢您提前给出答案!

这意味着jar中不包含这些类,如果您使用maven,您应该使用maven shade插件来包含这些类。

我实现了新插件,如图所示,但是使用了'junit:junit com/mongodb/MongoClient'`