Connecting to my database with java

I’ve been trying to access my atlas database from my java application and I haven’t found any methods that seem to work, I must be doing some small mistake. Could someone tell me how to do it, please? Sorry if this is the wrong category or if it has been posted before.

Hello @keenest, welcome to MongoDB community!

You can find tutorials regarding connecting to MongoDB database and performing operations on the database at the MongoDB Java Driver website. Here is the link to it: MongoDB Java Driver - Quick Start.

Also, here is another blog post: MongoDB & Java - CRUD Operations Tutorial

1 Like

Hi @keenest,

There are several possibilities for connection issues. Before connecting with a driver, I would try connecting using the latest version of a command-line tool like the mongo Shell or a GUI like MongoDB Compass. This will help confirm that your Atlas whitelist allows connections from your originating IP, and that you have the correct credentials and connection string to access your deployment. After connecting to your Atlas cluster you should try inserting or querying data to ensure the expected read/write permissions are available.

The Atlas documentation includes a guide to help Troubleshoot Connection Issues and guides to Connecting to a Cluster using various sources (Shell, Compass, Driver, …).

If you are able to connect to your cluster with Compass or the Shell, you can then look into any issues with your driver or code approach.

If you still need more assistance, please provide details for your application environment:

  • If you have been able to successfully connect to your cluster from the same originating IP using the Shell or Compass
  • Version of the MongoDB Java driver
  • Version of your JVM (output of java -version)
  • Snippet of code you are using to connect
  • The error message or result returned to your application


I tried using compass and it works. I’m using mongodb java driver 3.12.6. My java version is 1.8.0_241.

My IDE (eclipse) wants me to change “MongoClient” in:
MongoClient mongoClient = MongoClients.create(<connection string>);
to “com.mongodb.client.MongoClient”, and if I don’t it displays it as an error. My application doesn’t seem to like me using this method since it outputs this error:
java.lang.NoClassDefFoundError: com/mongodb/client/MongoClients

Hello @keenest,

The error java.lang.NoClassDefFoundError indicates that a class required by the code is not available at runtime (but the code compiled fine). You may have to review your setup using the MongoDB Java Driver. How have you configured your application?

I run com.mongodb.client.MongoClient mongoClient = MongoClients.create<connections string>); as soon as the application starts, if that is what you were asking.

@keenest, I mean how you have installed the Java Driver?

Here is an example of using MongoDB Java Driver I found online: Java MongoDB Example. It uses Eclipse IDE; I tried and it works fine, with driver version 3.12.7. There are lot of other details (which you may want to skip), but follow the below steps from the article:

  1. Java MongoDb Example
    2.3 Project Creation
  2. Application Building
    3.1 Maven Dependencies
    3.2 Java Class Creation

The following MongoDB Java code in the newly created class at step 3.2, ran fine from the main method:

try(MongoClient client = MongoClients.create("mongodb://localhost:27017")) {
    MongoDatabase database = client.getDatabase("test");
    MongoCollection<Document> collection = database.getCollection("test");
    Document doc = collection.find().first();

I didn’t create my project as a maven project, should I just be able to add a pom.xml manually and be able to use it?


  • Select the project -> Right-click and navigate to Configure -> Convert to Maven Project

  • This opens the Maven POM window. Select the defaults and Finish. This creates the pom.xml.

  • Add the following to the pom.xml:


Build and run the project.

Sorry for replying a bit late. I’ve converted my project to a maven project but it doesn’t seem to run the right way. When I run the application I get these messages in red a the top of my console:
Preformatted textSLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/C:/Users/Leo/.p2/pool/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [file:/E:/Program/eclipse/java-2020-06/eclipse/configuration/org.eclipse.osgi/5/0/.cp/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory] SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/C:/Users/Leo/.p2/pool/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [file:/E:/Program/eclipse/java-2020-06/eclipse/configuration/org.eclipse.osgi/5/0/.cp/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]

Does the project run or may be those messages could be ignored. I didn’t see any such messages in my console.

Those messages are just warnings (not errors). There is documentation online about those messages, e.g., SLF4J: Class path contains multiple SLF4J bindings..

As I know the Java driver version v3.12.7 uses java.util.logging, not SLF4J.

I’ve switched IDE from eclipse to intellij and after some tinkering the application works as it used to before I started using maven. Although the mongodb stuff still doesn’t work.

EDIT: The example you sent shows how to connect to a regular mongodb database. The problem I’m having is connecting to an atlas database, which uses a different method.

Hello, I’m Ody.
I’ve met problem in android studio when trying to connect to mongoDB atlas with my application using connectionString.
java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/naming/directory/InitialDirContext;
compass connection runs fine, but this error occurs when using application.
I’m stopped since few days…
Please, can you give some solutions to solve it??

ci dont know how to connect a mongodb compass to ecplise? can you please help me