简体   繁体   English

mongodb Java Driver build error: cannot access com.mongodb.client.result.InsertOneResult, class file not found

[英]mongodb Java Driver build error : cannot access com.mongodb.client.result.InsertOneResult, class file not found

I'm following the Installation and Quick Start on the latest (4.2.2) Mongodb Java drivers .我正在关注最新 (4.2.2) Mongodb Java 驱动程序的安装和快速入门 I'm getting this compile error:我收到此编译错误:

Error:(29, 29) java: cannot access com.mongodb.client.result.InsertOneResult class file for com.mongodb.client.result.InsertOneResult not found Error:(29, 29) java: cannot access com.mongodb.client.result.InsertOneResult class file for com.mongodb.client.result.InsertOneResult not found

This is part of a larger project, so could there be another version of mongo somewhere on the class path?这是一个更大项目的一部分,所以 class 路径上的某个地方是否有另一个版本的 mongo?
There is only one the pom, and it is this, straight from the Installation page:只有一个 pom,就是这个,直接来自安装页面:

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

I'm using IntelliJ, and in it's External Dependencies for mongo, it has these:我正在使用 IntelliJ,它是 mongo 的外部依赖项,它有这些:

org.mongodb:bson:3.8.2 org.mongodb:bson:3.8.2
org.mongodb:mongodb-driver-core:3.8.2 org.mongodb:mongodb-driver-core:3.8.2
org.mongodb:mongodb-driver-sync:4.2.2 org.mongodb:mongodb-driver-sync:4.2.2

Here's the code:这是代码:

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.Arrays;

public class MongoTest {

    public static void main(String[] args) {
        MongoTest mongoTest = new MongoTest();
        mongoTest.init();
    }

    public void init() {
        MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
        MongoDatabase myTestDb = mongoClient.getDatabase("myTestDb");
        MongoCollection<Document> collection = myTestDb.getCollection("test");

        Document doc = new Document("name", "MongoDB")
                .append("type", "database")
                .append("count", 1)
                .append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
                .append("info", new Document("x", 203).append("y", 102));

        collection.insertOne(doc);
    }
}

You need all three dependencies, but they should all be the same version, eg您需要所有三个依赖项,但它们都应该是相同的版本,例如

  • org.mongodb:bson:4.2.2 org.mongodb:bson:4.2.2
  • org.mongodb:mongodb-driver-core:4.2.2 org.mongodb:mongodb-driver-core:4.2.2
  • org.mongodb:mongodb-driver-sync:4.2.2 org.mongodb:mongodb-driver-sync:4.2.2

If you take a dependency only on mongodb-driver-sync, the others should be picked up transitively.如果你只依赖 mongodb-driver-sync,其他的应该被传递。 But it sounds like there is a conflict somewhere which is causing you to pick up older versions of bson and mongodb-driver-core.但听起来某处存在冲突,导致您选择旧版本的 bson 和 mongodb-driver-core。 You will need to determine where that conflict is coming from and address it.您将需要确定冲突的来源并解决它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB Java 驱动程序:无法解析类型 com.mongodb.client.model.InsertOneOptions。 它是从所需的 .class 文件间接引用的 - MongoDB Java Driver: The type com.mongodb.client.model.InsertOneOptions cannot be resolved. It is indirectly referenced from required .class files 无法访问com.mongodb.MongoClientSettings - MongoDB Java驱动程序3.7+ - Cannot access com.mongodb.MongoClientSettings - MongoDB Java driver 3.7+ Java:无法访问com.mongodb.ServerAddress-Java未连接到MongoDB - java: cannot access com.mongodb.ServerAddress - java not connecting to MongoDB 未找到com.mongodb.ServerAddress的类文件 - class file for com.mongodb.ServerAddress not found mongoDB java驱动程序出错 - Error with mongoDB java driver MongoDB Java驱动程序错误 - Mongodb java driver error com.allanbank.mongodb Java Mongodb异步驱动程序错误-无法引导与MongoDB服务器的连接 - com.allanbank.mongodb Java Mongodb Async driver error - Could not bootstrap a connection to the MongoDB servers mongodb-java-driver构建失败,并带有:bson:clirr“无法转换对象&#39;文件集合&#39;” - mongodb-java-driver build fails on gradle with :bson:clirr “Cannot cast object 'file collection'” Could not initialize class com.mongodb.connection.MongoQueryAnalyzer using mongodb Java driver - Could not initialize class com.mongodb.connection.MongoQueryAnalyzer using mongodb Java driver java:无法访问 com.google.auth.Credentials 未找到 com.google.auth.Credentials 的类文件 - java: cannot access com.google.auth.Credentials class file for com.google.auth.Credentials not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM