0

I am migrating a jar from using the 3.0 version of mongo driver to 5.1. I am using java SE 11. In my local environment, the tests work ok, but when in the web environment, I get the following error:

java.lang.NoSuchMethodError: 'void com.mongodb.MongoClient.< init >(java.util.List, java.util.List, com.mongodb.MongoClientOptions)' at deployment.VUCE.war//com.genexusconsulting.mongodb.GXCMongoDBConector.createClient(GXCMongoDBConector.java:811) at deployment.VUCE.war//com.genexusconsulting.mongodb.GXCMongoDBConector.connect(GXCMongoDBConector.java:106)

The line that is referring to has the following code:

    List<ServerAddress> seeds = new ArrayList<ServerAddress>();
    seeds.add(new ServerAddress(host, port));
    CodecRegistry codecRegistry = CodecRegistries.fromRegistries(CodecRegistries.fromProviders(new QueryResponseCodecProvider()), MongoClientSettings.getDefaultCodecRegistry());
    ConnectionString cnn = new ConnectionString(mongoUri);
    /* here there be error */
    MongoClientSettings options = MongoClientSettings.builder().
                    applyConnectionString(cnn).
                    applyToClusterSettings(builder -> builder.hosts(seeds)).
                    codecRegistry(codecRegistry).build();
    /* here there be error */

The pom file includes:

<!-- https://mvnrepository.com/artifact/org.mongodb/bson -->
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>bson</artifactId>
    <version>5.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-core -->
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-core</artifactId>
    <version>5.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-sync -->
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-sync</artifactId>
    <version>5.1.2</version>
</dependency>

For which, the files that I include on my lib folder are: bson-5.1.2.jar, bson-record-codec-5.1.2, mongodb-driver-core-5.1.2.jar and mongodb-driver-sync-5.1.2.jar, while also removing all previous versions. The code works fine in my local tests, but it fails when I include it in a test environment.

1
  • How are you deploying the artifact in test web environment? I had a similar issue a long time ago while dealing with EAR applications that were deployed as exploded archives. In that case, we had to check the directory where the dependency jars were copied, and make some manual cleaning of the old ones, because they were all included in classpath. Commented Nov 4, 2024 at 13:16

1 Answer 1

0
+50

I will do my best to help you, because I see that you haven't received any help yet. Hope to be of help.

The first thing I would say is to check if you have to make changes to your code (Connect to MongoDB) as you have updated the driver version. Here you have another link to see if you are using any deprecated class/method. For example, I see the following in the exception you have passed com.mongodb.MongoClient which is now com.mongodb.client.MongoClient. So it looks your code is still compatible with 3.X version instead of the 5.X version.

If after this you are still having problems, here you have a link to another answer, in case it helps you.

Note: With the command mvn dependency:tree (link) you can see the dependency tree for your project and ensure if any library is bringing in any older version.

2
  • I appreciate your help, but my code is indeed pointing to com.mongodb.client.MongoClient, so I don't understand where that reference is. Also, my dependency tree looks something like this: GXCMongoDBConector:GXCMongoDBConector:jar:1.0 ... +- org.mongodb:bson:jar:5.1.2:compile +- org.mongodb:mongodb-driver-core:jar:5.1.2:compile \- org.mongodb:bson-record-codec:jar:5.1.2:runtime +- org.mongodb:mongodb-driver-sync:jar:5.1.2:compile +- org.mongodb:mongodb-driver-legacy:jar:5.1.2:compile +- joda-time:joda-time:jar:2.8.1:compile ... Commented Nov 1, 2024 at 18:05
  • @GustavoMoreira As a last tip I would say to perform a clean build of the project to ensure that only the latest dependencies are included. mvn clean install.
    – Jos3lu
    Commented Nov 2, 2024 at 22:41

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