gpt4 book ai didi

java - 无法使用 mongodb Java 驱动程序初始化类 com.mongodb.connection.MongoQueryAnalyzer

转载 作者:太空宇宙 更新时间:2023-11-04 09:19:00 25 4
gpt4 key购买 nike

所以,我正在制作这个 java 程序来尝试一些基本的 mongodb 东西。

package sample;

import com.mongodb.DBObject;
import com.mongodb.Cursor;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;

public class Main {
public static void main(String[] args) {

MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("Species");
DBCollection col = db.getCollection("asdfg");
System.out.println("connexion made");
Cursor cursor=col.find();
System.out.println(cursor.next());

}
}

在集合中,我已经添加了一些 JSON 文档条目(大约 7 个)数据输入肯定没有问题(忽略打印语句中连接的拼写错误,我这样做是为了好玩)

好的,所以代码工作正常,直到 Cursorcursor=col.find(); 但当我在代码中包含此行时显示大量错误System.out.println(cursor.next());

这是我收到的代码错误

"C:\Program Files\Java\jdk1.8.0_111\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2.3\lib\idea_rt.jar=53479:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;C:\Users\mysti\Documents\untitled\out\production\untitled;C:\Users\mysti\Documents\mongo-java-driver-3.3.0-query-log-max-size.jar" sample.Main
Oct 27, 2019 9:11:54 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
connexion made
Oct 27, 2019 9:11:54 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Oct 27, 2019 9:11:54 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:1, serverValue:1951}] to localhost:27017
Oct 27, 2019 9:11:54 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 11]}, minWireVersion=0, maxWireVersion=7, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=799200}
Oct 27, 2019 9:11:54 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:1952}] to localhost:27017
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.mongodb.connection.MongoQueryAnalyzer
at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:222)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:216)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:207)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:113)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:488)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:483)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:245)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:218)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:483)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:80)
at com.mongodb.Mongo.execute(Mongo.java:818)
at com.mongodb.Mongo$2.execute(Mongo.java:805)
at com.mongodb.DBCursor.initializeCursor(DBCursor.java:851)
at com.mongodb.DBCursor.hasNext(DBCursor.java:152)
at com.mongodb.DBCursor.next(DBCursor.java:172)
at com.mongodb.DBCursor.next(DBCursor.java:60)
at sample.Main.main(Main.java:16)

Process finished with exit code 1

每当我尝试执行迄今为止所做的任何类型的读取操作时,都会发生类似的事情,而我可以轻松地添加数据而不会出现任何问题。请帮助我,我是新手。

最佳答案

总结评论中写的所有内容。

如果您在类中看到此类错误,请尝试更改 maven/gradle 项目中 mongodb java 驱动程序依赖项的较新版本,或手动为其添加另一个外部 jar。

对于 Maven:

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

对于 Gradle:

dependencies {
compile 'org.mongodb:mongodb-driver-sync:3.10.1'
}

作为替代方案,您还可以从 sonatype 下载 mongodb-driver-sync jar .

详细示例可以在驱动程序的 mongodb 文档 3.10 中找到。就我个人而言,我强烈建议您使用 stable versions如 3.10.2、3.8.2、3.6.4。

关于java - 无法使用 mongodb Java 驱动程序初始化类 com.mongodb.connection.MongoQueryAnalyzer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58580910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com