Issue
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:
<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(String[] args) {
String connectionString = "mongodb+srv://admin:[email protected]/<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:
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.
Solution
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).
Answered By - TorusWithSprinkles