Intellij Idea 尝试将Java连接到Mongodb Atlas时出现NoClassDefFoundError

ddhy6vgd  于 2023-11-16  发布在  Java
关注(0)|答案(1)|浏览(133)

我尝试使用Java连接到MongoDB atlas,但出现以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/internal/connection/StreamFactoryHelper
    at com.mongodb.client.internal.MongoClientImpl.getStreamFactory(MongoClientImpl.java:235)
    at com.mongodb.client.internal.MongoClientImpl.createCluster(MongoClientImpl.java:227)
    at com.mongodb.client.internal.MongoClientImpl.<init>(MongoClientImpl.java:73)
    at com.mongodb.client.MongoClients.create(MongoClients.java:108)
    at com.mongodb.client.MongoClients.create(MongoClients.java:93)
    at com.mongodb.client.MongoClients.create(MongoClients.java:78)
    at com.mongodb.client.MongoClients.create(MongoClients.java:61)
    at org.example.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: com.mongodb.internal.connection.StreamFactoryHelper
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 8 more

字符串
下面是我的代码:

package org.example;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

public class Main {
    public static void main(String[] args) {
        MongoClient client = MongoClients.create("mongodb+srv://kkartik:<password>@agilecluster.8wtmt68.mongodb.net/?retryWrites=true&w=majority");

        MongoDatabase db = client.getDatabase("SampleDB");

        MongoCollection col = db.getCollection("sampleCollection");
    }
}


下面是我的pom.xml依赖:

<dependencies>
            <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongodb-driver-sync</artifactId>
                <version>4.11.0</version>
            </dependency>
    </dependencies>


任何帮助是赞赏!

2ic8powd

2ic8powd1#

我想你也需要这个:

<dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-core</artifactId>
        <version>4.11.0</version>
    </dependency>

字符串

相关问题