0

I'm working on a DB project and this class instantiation code below seems to be failing. I can't seem to figure out why. I've made sure to run mongod and start MongoDB. Does anyone have any tips on how to resolve this?

public DatabaseInterface() {
        this.mongoClient = new MongoClient("127.0.0.1", 27017); ## This line
        this.database = mongoClient.getDatabase("dbmsProjectDB");
        this.gson = new Gson();
    }

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/operation/ReadOperation
    at backend.DatabaseInterface.<init>(DatabaseInterface.java:54)
    at backend.main.main(main.java:8)
Caused by: java.lang.ClassNotFoundException: com.mongodb.operation.ReadOperation
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 2 more 
1
  • Which version of MongoDb driver are you using? Commented Nov 21, 2021 at 17:34

2 Answers 2

1

use MongoClients.create(uri) instead

String uri = "mongodb://localhost";
MongoClient mongoClient = MongoClients.create(uri);
MongoDatabase database = mongoClient.getDatabase("dbmsProjectDB");
Sign up to request clarification or add additional context in comments.

Comments

0

The message tells you that he is not able to find the class com.mongodb.operation.ReadOperation. This means that this class is no longer available in this location.

Upgrade the version of your MongoDb Java Driver to the 4.X.X versions. Beginning from those versions, the path of the ReadOperation class has changed to another location : com.mongodb.internal.operation.ReadOperation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.