5

I am trying to connect MongoDB with Java. This is my code

    MongoCredential mongoCredential;
    mongoCredential = MongoCredential.createCredential(<uname>, <dbname>, <password>);
    List<ServerAddress> list = Arrays.asList(new ServerAddress("localhost", 27017));

    MongoClientSettings.Builder mongoClientSettingsBuilder = MongoClientSettings.builder()
            .credential(mongoCredential)
            .applyToClusterSettings(builder -> builder.hosts(list));

    MongoClientSettings settings = mongoClientSettingsBuilder.build();      

Executing this I get the error

Exception in thread "main" java.lang.NoSuchMethodError: com.mongodb.connection.DefaultClusterFactory.createCluster(Lcom/mongodb/connection/ClusterSettings;Lcom/mongodb/connection/ServerSettings;Lcom/mongodb/connection/ConnectionPoolSettings;Lcom/mongodb/connection/StreamFactory;Lcom/mongodb/connection/StreamFactory;Ljava/util/List;Lcom/mongodb/event/CommandListener;Ljava/lang/String;Lcom/mongodb/MongoDriverInformation;Ljava/util/List;)Lcom/mongodb/connection/Cluster;

I do not know what is the reason for this. Can someone help me out with this

7
  • Did you loaded the MongoDB Java Driver? And which version did you loaded? Did you added it to your classpath? Commented Mar 25, 2020 at 13:21
  • Yes I have loaded the driver. I am using mongodb-driver-3.6.4 Commented Mar 25, 2020 at 13:31
  • Maybe try with an update to 4.0.1 Commented Mar 25, 2020 at 13:32
  • Ok will try. Thank you! Commented Mar 25, 2020 at 13:55
  • I updated the driver version to 3.12.2, it worked. Thank you @Yonngan Commented Mar 26, 2020 at 4:30

2 Answers 2

4

This may be a conflict and confusion caused by several mongodb-driver jar file that system detected. I just solve the similar problem--NoSuchMethodError with disabling mongodb-driver-sync and using spring-boot-starter-data-mongodb in pom.xml like the following one:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.mongodb</groupId>-->
<!--            <artifactId>mongodb-driver-sync</artifactId>-->
<!--            <version>4.2.3</version>-->
<!--        </dependency>-->
Sign up to request clarification or add additional context in comments.

1 Comment

If you have no mongodb-driver-sync which dependency provides you MongoClient?
0

There are multiple mongodb-driver in your sbt file. In my case, it was

libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "4.5.1"
libraryDependencies += "org.mongodb.spark" %% "mongo-spark-connector" % "3.0.1"

Remove mongo-scala-driver dependency such that final build will look like this

libraryDependencies += "org.mongodb.spark" %% "mongo-spark-connector" % "3.0.1"

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.