0
    package com.sm.mongo;

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

    public class JavaMongoConnection {

    public static void main(String[] args) {

        //System.setProperty("jdk.tls.trustNameService", "true");

        MongoClientURI uri = new MongoClientURI(
                "mongodb+srv://admin:admin123@cluster0-bkruu.mongodb.net/test?retryWrites=true&w=majority");

            MongoClient mongoClient = new MongoClient(uri);
            MongoDatabase database = mongoClient.getDatabase("test");

    }
}

This is my code, and whenever I run this code, I get the error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI

I looked at java.lang.NoClassDefFoundError when using MongoDB driver and I tried their solution where I went to Run Configurations --> Dependencies and looked under Classpath Entries to make sure that I have bson-xxx.jar, mongodb-driver-xxx.jar, and mongodb-driver-core-xxx.jar listed. I have these listed and despite this, I keep getting the same error as in that stackoverflow post.

Any help would be appreciated.

sidgate
  • 14,650
  • 11
  • 68
  • 119
Madhu Sharma
  • 562
  • 4
  • 9
  • 1
    you need `mongo-java-driver` dependency – sidgate Jun 16 '20 at 06:56
  • 1
    Does this answer your question? [Why am I getting a NoClassDefFoundError in Java?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java) – leopal Jun 16 '20 at 07:19

1 Answers1

2

com/mongodb/MongoClientURI class is present in mongo-java-driver jar. https://mongodb.github.io/mongo-java-driver/

Including the above jar in classpath should fix the issue

sidgate
  • 14,650
  • 11
  • 68
  • 119