3

I have a problem with my MongoDB in Java. I cannot connect to it I always get an exception and could not resolve it. I use Maven Dependencies for that. I tried using other versions of the dependency.

I also checked that the MongoDB is running. I started it with net start MongoDB. My code:

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;

public class MongoDBTest {

    public static void main(String[] args) throws Exception {
        MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://127.0.0.1:27017"));
    }

}

My dependency:

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

And the exception i got:

Exception in thread "main" java.lang.NoSuchMethodError: com.mongodb.ConnectionString.getThreadsAllowedToBlockForConnectionMultiplier()Ljava/lang/Integer;
at com.mongodb.MongoClientURI.getOptions(MongoClientURI.java:351)
at com.mongodb.Mongo.createCluster(Mongo.java:724)
at com.mongodb.Mongo.<init>(Mongo.java:312)
at com.mongodb.Mongo.<init>(Mongo.java:308)
at com.mongodb.MongoClient.<init>(MongoClient.java:326)
at schlueting.arbeiten.MongoDBTest.main(MongoDBTest.java:9)
2
  • Please follow instruction from this link: Installation - Java Driver. Check your Maven dependency. And, Connecting. Commented Oct 27, 2020 at 10:33
  • 1
    It could help if you include which MongoDB version you are using. Also writing the versions for Java and Maven wouldn't hurt. Commented Oct 27, 2020 at 11:41

1 Answer 1

2

The solution was to add the following to the Maven dependencies:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-sync</artifactId>
    <version>3.10.1</version>
</dependency>

That fixed the Exception and I could connect to the MavenDB.

Btw. I was using MongoDB 4.4 and JDK 11.

Sign up to request clarification or add additional context in comments.

Comments

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.