Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with connecting to MongoDB on Java

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)
like image 561
Christopher Schlüting Avatar asked Sep 23 '26 06:09

Christopher Schlüting


1 Answers

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.

like image 59
Christopher Schlüting Avatar answered Sep 24 '26 19:09

Christopher Schlüting