1

I tried to use MongoDB java driver on Ubuntu 14.04.

The program indeed can pass the compile stage, but when running, it just has this error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClient
    at App.main(App.java:23)
Caused by: java.lang.ClassNotFoundException: com.mongodb.MongoClient
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

My Java version is 1.7, and I indeed tried 1.6, but same error happened. My compile command is:

javac App.java -classpath mongo-java-driver-2.13.0.jar

And I run the program using command:

java App

Below is my Java code:

import com.mongodb.BasicDBObject;
import com.mongodb.BulkWriteOperation;
import com.mongodb.BulkWriteResult;
import com.mongodb.Cursor;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ParallelScanOptions;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;

import java.util.List;
import java.util.Set;

public class App
{
    public static void main(String[] args)
    {
        System.out.println("----- Program Start -----");
        try {
            MongoClient mongoClient = new MongoClient("localhost" , 27017);
            DB db = mongoClient.getDB("demo");
            System.out.println("Connect to database successfully.");

            DBCollection coll = db.getCollection("test");
            BasicDBObject doc = new BasicDBObject();
            for(int i = 0; i < 10; i++) {
                doc.append("A" + Integer.toString(i), 12.56);
            }

            System.out.println(doc);
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": ");
            e.getMessage();
        }
    }
}

Could anyone please help? Thanks!!

1
  • Besides, the program will output line "----- Program Start -----", which means it runs into the main method. I also tried another version of driver but it still doesn't work. Commented Feb 1, 2015 at 1:01

1 Answer 1

2

The classpath is needed for both the compilation and the execution. The following invocation should work

java App -classpath mongo-java-driver-2.13.0.jar
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! But how can I make it work in maven? The problem originally happens in Maven actually.
For that you'd want to make a so called fat jar. See another Stack Overflow answer.
Thanks. But today I still found the command do not work. The same error happened even when I use your command.
I just changed to Windows platform and it seems to work with maven and Intellij. Any way, thank you very much!
Sorry just forget to choose your answer. But the fat jar really works in Maven. Thanks for help!

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.