简体   繁体   English

“错误:(3,19) java: 包 com.mongodb.client 不可见” - 将 MongoDB 与 IntelliJ 中的 Maven JavaFX 项目连接时出错

[英]"Error:(3,19) java: package com.mongodb.client is not visible" - Error connecting MongoDB with a Maven JavaFX project in IntelliJ

I'm working on a JavaFX project in IntelliJ using Maven, and now I'm trying to get it to connect to MongoDB.我正在使用 Maven 在 IntelliJ 中处理 JavaFX 项目,现在我正试图让它连接到 MongoDB。 I'm following along an official video from MongoDB doing the same thing, and this is what I added so far.我正在关注来自 MongoDB 的官方视频做同样的事情,这是我目前添加的内容。 The dependency to my pom file:对我的 pom 文件的依赖:

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

And the code to connect inside my public static void main :以及在我的public static void main连接的代码:

    public static void main(String[] args) {

        String connectionString = "mongodb+srv://admin:javaapp@orderscluster.wcn38.mongodb.net/<dbname>?retryWrites=true&w=majority";

        try (MongoClient mongoClient = MongoClients.create(connectionString)) {
            MongoIterable<String> strings = mongoClient.listDatabaseNames();
            MongoCursor<String> cursor = strings.cursor();

            while (cursor.hasNext()) {
                System.out.println(cursor.next());
        }
    }
    launch(); // This launches the JavaFX side of the app
}

This is the only code they added in the video, and at this stage it should properly print all the DB names to the console.这是他们在视频中添加的唯一代码,在此阶段它应该正确地将所有数据库名称打印到控制台。 However I'm getting these 4 errors:但是我收到了这 4 个错误:

Error:(3,19) java: package com.mongodb.client is not visible
Error:(4,19) java: package com.mongodb.client is not visible
Error:(5,19) java: package com.mongodb.client is not visible
Error:(6,19) java: package com.mongodb.client is not visible

Am I missing something?我错过了什么吗? I can't find much info on this particular error, and I followed the video instructions to a T. Thank you for any help!我找不到有关此特定错误的太多信息,因此我按照视频说明进行了操作。感谢您的帮助! I really appreciate it.对此,我真的非常感激。

So as per this doc from MongoDB I discovered I have to add a module declaration because I'm using Java version 9+.因此,根据MongoDB 的这个文档,我发现我必须添加一个模块声明,因为我使用的是 Java 9+ 版本。 I added this to my module-info.java and it successfully cleared those errors:我将此添加到我的 module-info.java 并成功清除了这些错误:

requires org.mongodb.driver.sync.client;

That does answer my original question, however I should note I am getting a new error when I try to run:这确实回答了我原来的问题,但是我应该注意,当我尝试运行时出现了一个新错误:

Error:(45, 48) java: cannot access com.mongodb.ConnectionString

In case anyone reading this happens to be doing the same thing and getting the same error, I'll keep this post updated with a solution to that or possibly a new solution overall (as I believe this error might be related to the first).万一读到这篇文章的人碰巧在做同样的事情并得到同样的错误,我会更新这篇文章,提供一个解决方案或一个新的整体解决方案(因为我相信这个错误可能与第一个有关)。

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

相关问题 java 控制台程序不存在包 com.mongodb.client - package com.mongodb.client does not exist for a java console program 包com.mongodb.client不存在 - package com.mongodb.client does not exist Java MongoDB:com.mongodb.DB和com.mongodb.client有什么区别 - Java MongoDB: What is the difference between com.mongodb.DB and com.mongodb.client 在SpringBoot提供的com.mongodb.client package中找不到MongoClient class - Cannot find the MongoClient class in the com.mongodb.client package provided by SpringBoot 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 Java 6-&gt; mongodb maven依赖项错误:对等方未通过身份验证 - java 6 -> mongodb maven dependency error: peer not authenticated MongoDB Java错误java.lang.NoClassDefFoundError:com / mongodb / MongoClient - MongoDB java error java.lang.NoClassDefFoundError: com/mongodb/MongoClient Java:无法访问com.mongodb.ServerAddress-Java未连接到MongoDB - java: cannot access com.mongodb.ServerAddress - java not connecting to MongoDB Maven包不可见编译错误 - Maven package is not visible compile error IntelliJ中的Java项目没有错误,但通过Maven编译失败 - Java project in IntelliJ is error free but compilation fails via Maven
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM