1

I'm trying to add a simple document to a collection in a MongoDB database:

MongoClient mongoClient = new MongoClient();
         
MongoDatabase db = mongoClient.getDatabase("productsdb");
         
Document product = new Document();
product.append("id", newJSON.getString("id"))
    .append("name", newJSON.getString("name"))
    .append("brand", newJSON.getString("brand"))
    .append("price", newJSON.getString("price"))
    .append("description", newJSON.getString("description"));
         
         
MongoCollection collection = db.getCollection("products");
collection.insertOne(product);
mongoClient.close();

I'm not getting any errors when compiling, but when running I get the following error: java.lang.NoClassDefFoundError: com/mongodb/MongoClient.

I made sure to add the latest jar files to my build path, these being bson-3.4.2.jar, mongodb-driver-3.4.2.jar and mongodb-driver-core-3.4.2.jar.

I read that the problem might lie in a missing or wrong classpath variable in the MANIFEST file of one or all of these jar files, but I'm not sure how to change these.

RobVH
  • 95
  • 1
  • 14
  • 1
    try adding jars to class using: Project->Build Path->Configure Buid Path-> Add External Jars. The MongoClient is available in mongodb-driver-3.4.2.jar – Atish Mar 19 '17 at 14:38

0 Answers0