ConnectionString class not found

When I try to run my Gradle project it gives the java.lang.NoClassDefFoundError: com/mongodb/ConnectionString error, I added org.mongodb:mongodb-driver-sync:4.8.1 as a dependency and my IntelliJ isn’t giving any errors. Anyone ran into this problem before? Here’s my code:

        ConnectionString connectionString = new ConnectionString("");
        MongoClientSettings settings = MongoClientSettings.builder()
                .applyConnectionString(connectionString)
                .serverApi(ServerApi.builder()
                        .version(ServerApiVersion.V1)
                        .build())
                .build();
        MongoDatabase database = MongoClients.create(settings).getDatabase("test");
        MongoCollection<Document> collection = database.getCollection("test");
        System.out.println(collection.find(Filters.eq("63b89b556a9c6e5ae7e558be")).first().getString("test"));

(Connection string is correct, I just didn’t want to leak it)

Hello @Jonathan_Jackson ,

Welcome to The MongoDB Community Forums! :wave:

I believe the most common reason for this error is that it can’t find the class that connects to MongoDB, perhaps you’re missing some imports or classpath?

If the issue you’re trying to solve is connecting to MongoDB, you might want to check the code examples in Java sync - Connect to MongoDB and see if it works for you.

Regards,
Tarun

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.