java.lang.NoClassDefFoundError when using MongoDB driver java.lang.NoClassDefFoundError when using MongoDB driver mongodb mongodb

java.lang.NoClassDefFoundError when using MongoDB driver


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.


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.


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>