3

I'm using MongoDB for a small project, but I'm getting this error when trying to use the function insertOne(Document) in the MongoDB Java Driver:

The type com.mongodb.client.model.InsertOneOptions cannot be resolved. It is indirectly referenced from required .class files

I configured the Java Driver just as this guide said: https://docs.mongodb.com/getting-started/java/client/

I also tried to create a new project, to no avail.

The MongoDB version I'm using is 3.2.6, and the MongoDB Java Driver version I'm using is 3.2.2. My JDK version is 1.8.0_91. The IDE I'm using is Eclipse Mars 2 (I don't know if that matters).

Thanks in advance.

Edit: This is a small piece of code I made for testing the Java Driver: http://pastebin.com/SGj0mXwh The problem is in the last line of the "addCompletedQuiz" function.

The only libraries I added to the project are the BSON 3.0.4 library and the MongoDB Java Driver 3.2.2 library, so I don't think this is a "JAR Hell" problem.

Also, I already tried to redownload and readd the Java Driver to no avail.

3
  • Can you attach the code where you are trying to insert the document? Also, check in your project properties whether multiple editions of the same libraries exist in it. It looks like a "JAR Hell" related problem. Commented May 6, 2016 at 21:40
  • Added the code and the libraries in the original post. Commented May 7, 2016 at 14:24
  • Adding mongo-java-driver-3.2.2.jar fixed the problem. Thanks PiXel1225! Commented May 8, 2016 at 13:11

1 Answer 1

1

Based on the code you've provided, I suggest that you should add the server name and the port when creating the MongoClient instance like this:

public Mongo() {
    String serverName = ""; //Usually it's 'localhost'
    String serverPort = ""; //Usually it's '8080'
    try {
        mongoClient = new MongoClient(serverName, serverPort);
        db = mongoClient.getDatabase("IFHKServer");
    } catch (MongoClientException e) {
        System.err.println("Error connecting to MongoDB Client.");
        Logger.getLogger(Mongo.class.getName()).log(Level.SEVERE, null, e);        }
}

Also, consider creating a method that will terminate the connection to MongoDB instance, when you are finished inserting new documents.

public final void closeMongoDBConnection() {
    try {
        mongoClient.close();
    } catch (Exception e) {
        System.err.println("Error in terminating connection");
        Logger.getLogger(Mongo.class.getName()).log(Level.SEVERE, null, e);
    }
}

The insertion code seems good as far as I can see.

General tips:

  • If you are attempting to insert, update or delete documents in MongoDB, always surround your code into try-catch blocks.
  • Do not omit the public/private/protected identifier from your methods/class fields, unless you want to restrict using your code only to your class/package. Assuming that your project includes classes in other packages that employ the code in Mongo class, declaring a method with no modifier permits the use of it outside the package that this class belongs to.
  • Avoid using hardcoded names for things that are anytime subject to change, like the name of your collection or a field in a document. You can transfer all these values into an external .properties file and read them using the Properties class. In that way your code will be more flexible in external changes, without rebuilding your entite project.

EDIT

Aside from the BSON Library, you should have the following three:

  • mongodb-driver-3.0.4.jar
  • mongodb-driver-async-3.0.4.jar
  • mongodb-driver-core-3.0.4.jar
Sign up to request clarification or add additional context in comments.

1 Comment

The above answer solved my error related with MongoClient mongoClient = new MongoClient(uri); Error : The type com.mongodb.ServerAddress cannot be resolved. It is indirectly referenced from required .class files

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.