12

I am using mongodb java driver 3.0.2 (I tried 3.0.1 and 3.0.0 but I get the same error).

This is the error that I get: The type org.bson.Document cannot be resolved. It is indirectly referenced from required .class files

It happens on the second line of the following code:

MongoDatabase db = mongoClient.getDatabase("db");
db.getCollection("");

As I understand the second line uses code that exists in org.bson.Document and it cant find it. But where can I find it?

************** EDIT ************ I know added the bson jar and it has the org.bson package BUT there is not org.bson.Document class inside this package...

1
  • Are you sure you are using the Mongo Java Driver which contains several libraries, and no the MongoDB Driver for Java? see my answer below.
    – NDB
    Nov 20, 2015 at 8:31

5 Answers 5

17

I believe you have downloaded the MongoDB-Driver for Java, and not the Mongo-Java-Driver.

Easiest way is to download the mongo-java-driver-3.x.x.jar library, available at http://mongodb.github.io/mongo-java-driver/

It contains the following packages:

  • MongoDB Driver
  • MongoDB Async Driver
  • BSON Library
  • Core Driver

If you install the MongoDB driver only , then you need to download BSON Library separately.

2
  • 1
    The mongoDB installation guide for the Java driver says "There are two Maven artifacts available in the 3.4 release. The preferred artifact for new applications is mongodb-driver however, we still publish the legacy mongo-java-driver uber-jar." Aug 22, 2017 at 17:32
  • i am using mongo-java-driver 3.7.1, still i am unable to import the libraries. Oct 12, 2018 at 6:37
5

Document class exist since from 3.0.0 and not in MongDB jar, it's in BSON jar file. You can download from https://oss.sonatype.org/content/repositories/releases/org/mongodb/bson/3.0.2/bson-3.0.2.jar

Reference:

public class Document extends Object implements Map, Serializable, Bson A representation of a document as a Map. All iterators will traverse the elements in insertion order, as with LinkedHashMap. Since: 3.0.0

http://api.mongodb.org/java/3.0/org/bson/Document.html

2

If you are using eclipse and maven change the dependency in pom.xml as follows:-

  <dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.4.2</version>
  </dependency>
1

You will need to download the bson library (v3.0.2) and add it to your classpath : https://oss.sonatype.org/content/repositories/releases/org/mongodb/bson/3.0.2/bson-3.0.2.jar

1
0

You should check if this package is in the classpath: http://api.mongodb.org/java/current/org/bson/package-summary.html

2
  • Well, that's really strange... Are you sure that it is included in the classpath?
    – pinturic
    Jun 12, 2015 at 11:56
  • Yes it is it. The class Document is simply not there... I have no idea why Jun 12, 2015 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.