我正嘗試使用servlet上的Java驅動程序連接到mlab上託管的MongoDB數據庫。當使用MongoDB驅動程序時出現java.lang.NoClassDefFoundError
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:[email protected]:43109/readings");
MongoClient client = new MongoClient(uri);
MongoDatabase db = client.getDatabase(dbName);
MongoCollection<Document> readings = db.getCollection(collection);
Document doc = Document.parse(loc);
readings.insertOne(doc);
client.close();
}
}
是我收到以下錯誤的問題: java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI
我看了一個答案(How to resolve ClassNotFoundException: com.mongodb.connection.BufferProvider?),突出對我來說,我需要其他的罐子,因爲我已經下載了他們但是我仍然得到這個錯誤。
我使用Eclipse並將三個瓶子添加到構建路徑中,通過右鍵單擊該項目然後在Build Path -> Configure build path -> Java build path -> libraries -> add external JARs
之後導航菜單。
這是正確的做法嗎?還有什麼我應該做的呢?
每個驅動程序都列在「類路徑選項卡」 - >「用戶條目」 - >「MyProject(默認類路徑)」下。然後我添加到jar到服務器類路徑,並解決它。謝謝 –