MongoDB Runtime Exception Throwing NoClassDefFoundError

Hadif Hatta :

I am trying to connect to the MongoDB database using...

MongoClient client = MongoClients.create(uri);

There is no error come out when building the application, but the error occurs only in runtime.

java.lang.RuntimeException: java.lang.NoClassDefFoundError: com/mongodb/client/MongoClients
   at ...

I have added the required dependencies on the project structure as follows:

enter image description here

Ps: I am not so familiar with Java project, not so sure what I was doing but I hope you guys understand my current situation, thank you!

Ananthapadmanabhan :

The issue occurs only at runtime which means that these jars are not resolved during runtime. So what you could do is to edit the Runtime configurations to add these jars to the runtime through IntelliJ.(project context menu -> "Run As" -> "Run Configurations...") then edit it to add the jars bson-X.X.X.jar and the other two mongodb-driver-x-x-x.jar and mongodb-driver-core-x-x-x.jar . If you are deploying in an external server make sure these jars are copied over to the server during deployment.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=396556&siteId=1