How to resolve ClassNotFoundException: com.mongodb.connection.BufferProvider?
Asked Answered
Z

5

17

I am trying to write simple java code which shows the MongoDB collections on console. I have added mongodb-driver-3.0.0.jar in my classpath.

But when I try to execute the code, it is giving me following error at the line of Database connection:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/connection/BufferProvider at com.chintan.app.MongoDbJdbc.main(MongoDbJdbc.java:12) Caused by: java.lang.ClassNotFoundException: com.mongodb.connection.BufferProvider at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more

Following is my code snippet:

public static void main(String[] args) 
    {
        MongoClient mongoClient = new MongoClient("localhost", 27017); //Exception

        @SuppressWarnings("deprecation")
        DB db = mongoClient.getDB("mydb");
        System.out.println("Database connection successfull.");
        ...
        ...
    }

What is the problem here? Do I need to add another jar in classpath or there is some version problem?

Zahn answered 19/4, 2015 at 11:37 Comment(1)
can you show your imports?Dewain
S
37

On the mongo driver page here: http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/installation-guide/#mongodb-driver

You'll see the following text:

Note: mongodb-driver requires the following dependencies: bson and mongodb-driver-core

So you need all of the following jars to make this work:

mongodb-driver-3.0.1.jar, mongodb-driver-core-3.0.1.jar, bson-3.0.1.jar

Which can be downloaded from here: https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver/3.0.1/ https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver-core/3.0.1/ https://oss.sonatype.org/content/repositories/releases/org/mongodb/bson/3.0.1/

Sagacious answered 21/5, 2015 at 2:16 Comment(3)
For later readers: Pay attention to take the newest version of the jars. Just navigate to the parent directories and pick the newest ones...Liv
Also, for later readers, this is the correct answer, I just don't know why it wasn't marked as the right one.Dualistic
This saved me some time. ThanksMichel
C
3

The following java driver contains the BufferProvider class:

http://mvnrepository.com/artifact/org.mongodb/mongo-java-driver/3.0.0

Cirque answered 19/4, 2015 at 11:45 Comment(0)
L
0

This class is in the mongodb-driver-core-3.0.0 jar file which is required by mongodb-driver-3.0.0.jar. You can see java drivers dependencies in the POM file associated with this jar.
I think you have to use a dependency manager to automatically add MongoDB (and other components) transitive dependencies to your project (maven, gradle, ...).

Lyautey answered 19/4, 2015 at 12:0 Comment(0)
A
0

If you don't have maven project, just include mongo-java-driver-3.6.1.jar (this jar is only for Java) in your project no need to include other jars.

Anthe answered 19/1, 2018 at 13:38 Comment(0)
F
0

include mongo-java-driver-3.11.0.jar file in class path not in Module path.

Fermentative answered 20/8, 2019 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.