1

Mongo DB server is running on local machine. I can open mongo shell and query the database without any issue.

The java file compiles without any errors. However while executing I get error.

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClient
        at mongoTest.main(mongoTest.java:26)
Caused by: java.lang.ClassNotFoundException: com.mongodb.MongoClient
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

Sample Java code which I wrote is shown below. When I execute the file at command prompt

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;

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

import org.bson.Document;
import java.util.Arrays;
import com.mongodb.Block;

import com.mongodb.client.MongoCursor;
import static com.mongodb.client.model.Filters.*;
import com.mongodb.client.result.DeleteResult;
import static com.mongodb.client.model.Updates.*;
import com.mongodb.client.result.UpdateResult;
import java.util.ArrayList;
import java.util.List;

public class mongoTest {

    public static void main(String[] args) {

        MongoClient c = new MongoClient("localhost", 27017);
        MongoDatabase database = c.getDatabase("video");
        MongoCollection collection = database.getCollection("movieDetails");

        System.out.println(collection.count());

    }
}
4
  • Looks like a problems with your classpath. How are you executing the program? Through an IDE or via the command-line? Commented May 11, 2018 at 0:38
  • Compiled the java file at command line. No errors during compilaton. Tried to execute the same using command java mongoTest Commented May 11, 2018 at 0:51
  • And, when you compiled your class, did you you specify a classpath option pointing to your MongoDB JARs? Commented May 11, 2018 at 1:02
  • Yes... FYI MongoDB Server version is 3.6.2 and Java 8 update 171 Compile statement at command line is - javac -cp .;C:\Java\jdk1.8.0_171\jre\lib\mongo\bson-3.6.2.jar;C:\Java\jdk1.8.0_171\jre\lib\mongo\mongodb-driver-3.6.2.jar;C:\Java\jdk1.8.0_171\jre\lib\mongo\mongodb-driver-core-3.6.2.jar;C:\Java\jdk1.8.0_171\jre\lib\mongo\mongo-java-driver-3.6.2.jar mongoTest.java No errors during compilation Commented May 11, 2018 at 1:05

1 Answer 1

2

You'd just need to run the class with something like:

java -cp <path-to-your-mongodb-jars> mongoTest

It's also standard Java practice to capitalise the name of your class, so MongoTest rather than mongoTest

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you.. that worked... Also regarding the java practices.. I will keep it in mind. Just started programming in java and mongo
If you haven't come across it already, check out the free online MongoDB course for Java developers at university.mongodb.com/courses/M101J/about. It's well worthwhile.
That was the first thing that I saw :) . I will be definitely doing that course when the next run starts on 29th of this month. Thank you

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.