Home > Back-end >  Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/operation/ReadOpera
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/operation/ReadOpera

Time:11-22

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 

CodePudding user response:

use MongoClients.create(uri) instead

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

CodePudding user response:

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

  • Related