dtech dtech - 3 months ago 23
Java Question

IntelliJ not loading transitive dependency in maven project

I've made a small library, lets call it

lib
. It dependends on another library,
sublib
which is available in Maven central:

lib/pom.xml
:

<dependencies>
<dependency>
<groupId>3rdparty</groupId>
<artifactId>sublib</artifactId>
<version>x</version>
</dependency>
</dependencies>


Now I'm trying to use
lib
in my project
proj
. I've set it as a dependency:

proj/pom.xml
:

<dependencies>
<dependency>
<groupId>mynamespace</groupId>
<artifactId>lib</artifactId>
<version>y</version>
</dependency>
</dependencies>


When I run
mvn exec:java -D exec.mainClass=mynamespace.proj.Main
the program runs fine.
However if I run it from IntelliJ, I get the following error:

java.lang.NoClassDefFoundError: 3rdparty/SomeSubLibClass
at mynamespace.SomeLibClass.method(SomeLibClass.java:100)


This seems to indicate that IntelliJ does not load the transitive
sublib
dependency. How can I fix this?

Answer

You can manually right click on the pom.xml file in the file tree and select maven > reimport.

Sometimes you'll see a popup saying "Maven projects need to be imported"; you should select Enable Auto-Import.

Maven auto import popup

This option can be found in Preferences > Maven > Importing > [x] Import Maven projects automatically (and is unchecked by default):

enter image description here