Jonathan Woollett-light
Jonathan Woollett-light

Reputation: 3261

Java with Maven in IntelliJ IDEA: How to properly import a dependency

I seem unable to import any of my added dependencies. In the following code (pom.xml) we see the JavaFX dependencies which work and the MongoDB dependency which I added myself and don't work.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jonathan.woollettlight</groupId>
    <artifactId>CS-235 A3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>12.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>12.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-sync</artifactId>
            <version>3.10.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.1</version>
                <configuration>
                    <mainClass>com.jonathan.woollettlight.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

The JavaFX import statements which work:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;

These do not work.

import org.mongodb.*;
import org.bson.*;
import com.mongodb.*;

With the import org.bson.*; giving the error: Package 'org.bson' is declared in module 'org.mongodb.bson', but module 'com.jonathan.woollettlight' does not read it. And import com.mongodb.*; giving Package 'com.mongodb' is declared in module 'org.mongodb.driver.sync.client', but module 'com.jonathan.woollettlight' does not read it

My file structure:

enter image description here

Expanded external dependencies for MongoDB.

enter image description here

I tried other dependencies and even followed this video (https://www.youtube.com/watch?v=NP6AnvZ0CTA) and that didn't work. I am unable to make any import statement on a dependency I've added work.

I should note the project was originally created via the Maven JavaFX archetype. To add the archetype use groupId org.openjfx, artifactId javafx-maven-archetypes and version 0.0.1.

I am at a loss as to what to do here any help would be greatly appreciated. Link to project uploaded on google drive if you're that curious/nice (https://drive.google.com/file/d/1WKNg1G81dpOhrtO9W3OyRkL-rv10fbvO/view?usp=sharing)

Upvotes: 1

Views: 1557

Answers (2)

Sachin
Sachin

Reputation: 2177

I just downloaded your project and i see the module-info.java contains only javafx.* requires.

requires javafx.controls;
requires javafx.fxml;

Try adding org.mongodb.bson you should be able to import or you can remove the module-info.java file for testing.

I tried both removing the module-info.java for testing and adding the requires statements, i was able to add the import statements such as import org.bson.*; I am also using intellij IDE.

module-info.java

module-info.java

App.java

App.java

Upvotes: 1

Mark Bramnik
Mark Bramnik

Reputation: 42541

First of all please make sure that the dependency that you've added indeed accessible by maven, even without IDE: just run something like: mvn package to see that maven downloads the dependency

Now, if the project was imported into idea by opening the pom.xml, intelliJ is supposed to build its internal data based on the information that was found in pom.xml

So the following might help:

Maven -> "Reimport all maven projects" - This will trigger the synchronization process

Settings -> Build, Execution,Deployment -> Build tools -> Maven: Maven home directory - intellij might be configured with a different maven than you think you use (has something bundled with ide). Your maven might be pre-configured with some custom properties, proxies, credentials, etc.)

File -> Close Project and then open it again, of course open a pom.xml (only if two items above didn't help). This can fix the situation if for some reason intelliJ went into issues of synchronization of its interal data structures with the data from pom.xml, happened a couple of times to me, so I mention this.

Upvotes: 0

Related Questions