title: java.lang.NoClassDefFoundError when using MongoDB driver
body: <p>I am trying to connect to a MongoDB database hosted on mlab using the Java driver on a servlet.</p>
  
  <pre><code>import org.bson.Document; 
  import com.mongodb.MongoClient;
  import com.mongodb.MongoClientURI;
  import com.mongodb.client.MongoCollection;
  import com.mongodb.client.MongoDatabase;
  
  public class MongoConnection {
  
      protected void connectToMongo(String loc){
  
          String dbName = "readings";
          String collection = "data";
  
          MongoClientURI uri = new MongoClientURI("mongodb://user:pass@ds143109.mlab.com:43109/readings");
          MongoClient client = new MongoClient(uri);
          MongoDatabase db = client.getDatabase(dbName);
  
          MongoCollection&lt;Document&gt; readings = db.getCollection(collection);
  
          Document doc = Document.parse(loc);
  
          readings.insertOne(doc);
  
          client.close();
      }
  }
  </code></pre>
  
  <p>The problem is I am getting the following error:
  <code>java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI</code></p>
  
  <p>I looked at one answer (<a href="https://stackoverflow.com/questions/29729331/how-to-resolve-classnotfoundexception-com-mongodb-connection-bufferprovider">How to resolve ClassNotFoundException: com.mongodb.connection.BufferProvider?</a>) that highlighted to me that I need other jars, I have since downloaded them however I am still getting this error.</p>
  
  <p>I am using Eclipse and adding the three jars to the build path, navigating through the menu by right clicking on the project then following <code>Build Path -&gt; Configure build path -&gt; Java build path -&gt; libraries -&gt; add external JARs</code>. </p>
  
  <p>Is this the right way to do it? Is there something else I am supposed to do as well/instead?</p>