Java Error when running mongodb program

378 views
Skip to first unread message

Zhongli Wu

unread,
Jan 30, 2015, 11:44:31 PM1/30/15
to mongod...@googlegroups.com
Hi All,

I've written the basic MongoDB driver program using Java. 

I use driver 2.13.0 to compile the code on Ubuntu 14.04: 

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

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();
 
}
   
}
}

It compiles all well with no error but when I run the program error happens:
----- Program Start -----
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:202)
 at java
.security.AccessController.doPrivileged(Native Method)
 at java
.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java
.lang.ClassLoader.loadClass(ClassLoader.java:306)
 at sun
.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java
.lang.ClassLoader.loadClass(ClassLoader.java:247)
 
... 1 more

This will also happens when I build the program using Maven.

I've tried Oracle Java 1.6 and 1.7 to compile the code. But the same error happened.
Could anyone please help? Thanks!

Jeff Yemin

unread,
Jan 31, 2015, 10:02:19 AM1/31/15
to mongod...@googlegroups.com
You say you compiled like this:

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

How did you try to run it?


Regards,
Jeff


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/70347990-bedb-4079-8de7-150e92abe80d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages