[英]"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.