java.lang.NoClassDefFoundError when using MongoDB driver
Asked Answered
S

4

8

I am trying to connect to a MongoDB database hosted on mlab using the Java driver on a servlet.

import org.bson.Document; 
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class MongoConnection {

    protected void connectToMongo(String loc){

        String dbName = "readings";
        String collection = "data";

        MongoClientURI uri = new MongoClientURI("mongodb://user:[email protected]:43109/readings");
        MongoClient client = new MongoClient(uri);
        MongoDatabase db = client.getDatabase(dbName);

        MongoCollection<Document> readings = db.getCollection(collection);

        Document doc = Document.parse(loc);

        readings.insertOne(doc);

        client.close();
    }
}

The problem is I am getting the following error: java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI

I looked at one answer (How to resolve ClassNotFoundException: com.mongodb.connection.BufferProvider?) that highlighted to me that I need other jars, I have since downloaded them however I am still getting this error.

I am using Eclipse and adding the three jars to the build path, navigating through the menu by right clicking on the project then following Build Path -> Configure build path -> Java build path -> libraries -> add external JARs.

Is this the right way to do it? Is there something else I am supposed to do as well/instead?

Snakemouth answered 8/2, 2017 at 19:30 Comment(1)
I had the issue java.lang.NoClassDefFoundError: com/mongodb/client/MongoClients using IntelijIDEA, solved by editing Run/Debug configuration.Anastomosis
V
2

You have java.lang.NoClassDefFoundError - that means your class is missed during runtime (not during build/compile time). So you should open your "Run Configurations" dialog for the project (project context menu -> "Run As" -> "Run Configurations...") and make sure you have bson-xxx.jar, mongodb-driver-xxx.jar, and mongodb-driver-core-xxx.jar somehow listed in Classpath tab. And yes, like Xavier Bouclet said - if you run it under application server - this jars should be added to your server's classpath.

Vise answered 8/2, 2017 at 19:54 Comment(1)
Each of the drivers are listed under the "Classpath tab" -> "User Entries" -> "MyProject (default classpath"). I then added to jars to the server classpath and this resolved it. ThanksSnakemouth
L
0

You have to make sure that the mongodb jars are exported to server if your call to the database are made from the servlet.

Check how you deploy your app on your local server dans make sure the jars are there.

Loring answered 8/2, 2017 at 19:51 Comment(0)
D
0

I was facing similar issue with my Mule 4 project.

Failed to invoke lifecycle phase "initialise" on object:

That was pointing to:

java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI

So I had to updated POM file in plugin section (mule-mave-plug, idk what would it be in java project):

<sharedLibraries>
    <sharedLibrary>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-legacy</artifactId>
    </sharedLibrary>
</sharedLibraries>
Diverge answered 4/5, 2021 at 9:58 Comment(0)
G
0

You can use in your POM to make it up and running and avoid no class def found error, apart from that we can also add below sync and async dependencies of mongo.

<!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-sync -->
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-sync</artifactId>
    <version>5.0.0</version>
</dependency>
    
<!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-reactivestreams -->
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-reactivestreams</artifactId>
    <version>5.0.0</version>
</dependency>
    
<!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-legacy -->
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-legacy</artifactId>
    <version>5.0.0</version>
</dependency>
Generosity answered 3/4 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.