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

huangapple go评论89阅读模式
英文:

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

问题

我正在使用IntelliJ和Maven在JavaFX项目上工作,现在我正在尝试使其连接到MongoDB。我正在按照MongoDB官方视频中的步骤进行操作,目前我已添加了以下内容到我的项目中。在我的pom文件中添加了依赖项:

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

以及在我的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(); // 这会启动JavaFX部分的应用
}

这是视频中添加的唯一代码,在这个阶段,它应该将所有数据库名称正确打印到控制台。然而,我遇到了以下4个错误:

错误:(3,19) java: 包com.mongodb.client不可见
错误:(4,19) java: 包com.mongodb.client不可见
错误:(5,19) java: 包com.mongodb.client不可见
错误:(6,19) java: 包com.mongodb.client不可见

我是否漏掉了什么?我在这个特定错误上找不到太多信息,我按照视频中的说明进行了操作。感谢您的任何帮助!我非常感谢。

英文:

I'm working on a JavaFX project in IntelliJ using Maven, and now I'm trying to get it to connect to MongoDB. I'm following along an official video from MongoDB doing the same thing, and this is what I added so far. The dependency to my pom file:

    &lt;dependency&gt;
        &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
        &lt;artifactId&gt;mongodb-driver-sync&lt;/artifactId&gt;
        &lt;version&gt;3.12.7&lt;/version&gt;
    &lt;/dependency&gt;

And the code to connect inside my public static void main:

    public static void main(String[] args) {

        String connectionString = &quot;mongodb+srv://admin:javaapp@orderscluster.wcn38.mongodb.net/&lt;dbname&gt;?retryWrites=true&amp;w=majority&quot;;

        try (MongoClient mongoClient = MongoClients.create(connectionString)) {
            MongoIterable&lt;String&gt; strings = mongoClient.listDatabaseNames();
            MongoCursor&lt;String&gt; 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:

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.

答案1

得分: 1

根据这篇MongoDB的文档,我发现因为我使用的是Java版本9及以上,所以我需要添加一个模块声明。我将下面的代码添加到了我的module-info.java文件中,并且成功地解决了这些错误:

requires org.mongodb.driver.sync.client;

这确实回答了我的最初问题,然而我需要注意一下,当我尝试运行时我遇到了一个新的错误:

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

如果有任何人阅读这篇文章并且遇到了相同的问题,我会持续更新这篇帖子,提供解决方案或者可能的整体新解决方案(因为我认为这个错误可能与第一个错误有关)。

英文:

So as per this doc from MongoDB I discovered I have to add a module declaration because I'm using Java version 9+. I added this to my module-info.java and it successfully cleared those errors:

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).

huangapple
  • 本文由 发表于 2020年10月16日 21:52:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64390557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定