Unable to connect MonogDB atlas to Java

I’m trying to connect to MongoDB atlas using Java and I’m getting the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/internal/connection/StreamFactoryHelper
    at com.mongodb.client.internal.MongoClientImpl.getStreamFactory(MongoClientImpl.java:235)
    at com.mongodb.client.internal.MongoClientImpl.createCluster(MongoClientImpl.java:227)
    at com.mongodb.client.internal.MongoClientImpl.<init>(MongoClientImpl.java:73)
    at com.mongodb.client.MongoClients.create(MongoClients.java:108)
    at com.mongodb.client.MongoClients.create(MongoClients.java:93)
    at com.mongodb.client.MongoClients.create(MongoClients.java:78)
    at com.mongodb.client.MongoClients.create(MongoClients.java:61)
    at org.example.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: com.mongodb.internal.connection.StreamFactoryHelper
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 8 more

Here’s my code:

package org.example;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class Main {
    public static void main(String[] args) {
        MongoClient client = MongoClients.create("mongodb+srv://kkartik:<password>@agilecluster.8wtmt68.mongodb.net/?retryWrites=true&w=majority");

        MongoDatabase db = client.getDatabase("SampleDB");

        MongoCollection col = db.getCollection("sampleCollection");
    }
}

Here’s my pom.xml dependancy:

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

Any help is appreciated!

Hey @kushagra_kartik,

Welcome to MongoDB Community forums!

The java.lang.NoClassDefFoundError generally indicates that a class is missing during runtime (not during the build/compile process). To address this, please open the “Run Configurations” dialog for your project and ensure that the mongodb-driver-xxx.jar is listed in the Classpath tab.

Also, if you’d like to reference code samples, please take a look at the Java - Quick Start Project.

Best regards,
Kushagra

Where do I download the mongo-driver jar from?

I see that the class “StreamFactoryHelper” is in “driver-core”:

Within your pom.xml file, add another dependency for “mongodb-driver-core” with a version matching the “mongodb-driver-sync” like this:

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

I am getting the same error despite adding the mongodb-driver-core dependancy to my pom.xml file.

Have you been able to find a solution for this ?

Check your Maven dependencies in classpath contain both the jars

mongodb-driver-sync-4.11.0.jar

mongodb-driver-core-4.11.0.jar

Else do a clean build and run the Main program again.