user12653669
user12653669

Reputation: 31

ERROR java.lang.NoClassDefFoundError: com/mongodb/MongoClient

so i got a very strange error message. Im currently working on a java web project with maven and testing the project with Eclipse and Tomcat. So I imported all the neccessary dependencys (mongo java driver, mongodb driver, mongodb driver core, bson and javax.servlet api), or so i thought. But still i'm getting this error over and over again.

If I run the code as part of a main method it works just fine...so im in the dark what could have caused this problem.

this is my MongoDB connector,

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();
    }
}

and this is part of my 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");
            }

Thanks for any answers in advance!

Upvotes: 0

Views: 1129

Answers (1)

Maxdola
Maxdola

Reputation: 1650

This means that the classes are not included in the jar, if you are using maven you should use the maven shade plugin to include those.

Upvotes: 0

Related Questions