使用 MongoDB 驱动时出现 java.lang.NoClassDefFoundError

问题描述 投票:0回答:4

我正在尝试使用 servlet 上的 Java 驱动程序连接到 mlab 上托管的 MongoDB 数据库。

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

我查看了一个答案(如何解决 ClassNotFoundException:com.mongodb.connection.BufferProvider?),它向我强调我需要其他 jar,我已经下载了它们,但我仍然收到此错误。

我正在使用 Eclipse 并将三个 jar 添加到构建路径,通过右键单击项目然后按照

Build Path -> Configure build path -> Java build path -> libraries -> add external JARs
浏览菜单。

这是正确的做法吗?我还应该做其他事情/代替吗?

java eclipse mongodb noclassdeffounderror mlab
4个回答
3
投票

您有

java.lang.NoClassDefFoundError
- 这意味着您的课程在运行时(而不是在构建/编译时)错过了。因此,您应该打开项目的“运行配置”对话框(项目上下文菜单 -> “运行方式” -> “运行配置...”)并确保您有 bson-xxx.jar、mongodb-driver-xxx。 jar 和 mongodb-driver-core-xxx.jar 以某种方式列在“类路径”选项卡中。是的,就像 Xavier Bouclet 所说 - 如果你在应用程序服务器下运行它 - 这个 jar 应该添加到你的服务器的类路径中。


0
投票

如果您从 servlet 调用数据库,则必须确保将 mongodb jar 导出到服务器。

检查如何在本地服务器上部署应用程序,并确保 jar 在那里。


0
投票

我的 Mule 4 项目也面临类似的问题。

Failed to invoke lifecycle phase "initialise" on object:

那指向:

java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI

所以我必须更新插件部分中的 POM 文件(mule-mave-plug,不知道 java 项目中会是什么):

<sharedLibraries>
    <sharedLibrary>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-legacy</artifactId>
    </sharedLibrary>
</sharedLibraries>

0
投票
You can use in your POM to make it up and running and avoid no class def found error, apart from that we can also add below sync and async dependencies of mongo.

<!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-sync -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-sync</artifactId>
            <version>5.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-reactivestreams -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-reactivestreams</artifactId>
            <version>5.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-legacy -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-legacy</artifactId>
            <version>5.0.0</version>
        </dependency>
© www.soinside.com 2019 - 2024. All rights reserved.