user9186277
user9186277

Reputation: 45

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

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 

Upvotes: 0

Views: 5704

Answers (2)

Harry Coder
Harry Coder

Reputation: 2740

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

Upvotes: 0

YuTing
YuTing

Reputation: 6629

use MongoClients.create(uri) instead

String uri = "mongodb://localhost";
MongoClient mongoClient = MongoClients.create(uri);
MongoDatabase database = mongoClient.getDatabase("dbmsProjectDB");

Upvotes: 1

Related Questions