Jake
Jake

Reputation: 15227

Maven. Transitive dependencies

My project P depends on dependency A which depends on dependency B. My project's pom.xml file includes A as a dependency, and its jar is included in P's classpath. However, there is a NoClassDefFoundError thrown at runtime of P, which stems from missing B jars.

Shouldn't Maven have downloaded these dependencies automatically?

Upvotes: 45

Views: 43501

Answers (4)

Emadpres
Emadpres

Reputation: 3747

I'm writing my own solution/situation since none of the other answers solved my problem.

In my case, my project (ME) was dependant on another project (LIB) which was dependant on another project (open-csv).

However, I noticed an older version of open-csv is already a dependency in my project, and probably this force the second library (LIB) use this older version of open-csv library, which does not contain a newer class used by LIB.

I solved the issue by updating the version of open-csv in my main project dependency list.

Upvotes: 0

physox
physox

Reputation: 323

In my case i forgot to start Eclipse with the -vm parameter, which should point to jdk/javaw.exe.

Even after 5 Years of Java EE programming you still make greenhorn mistakes...

Upvotes: -3

Pascal Thivent
Pascal Thivent

Reputation: 570595

My project P depends on dependency A [with a compile scope] which depends on dependency B [with a compile scope].

Unless B is an optional dependency of A, B should be a dependency of P with a "compile(*)" scope (see the table of Dependency Scope and read the note) and should thus be available at runtime.

However, there is a NoClassDefFoundError thrown at runtime of P, which stems from missing B jars.

Since you're running the project under Eclipse, the class path is setup for you so I'll exclude a mistake at this level. This leaves us with the case of the optional dependency.

PS: A very useful tool to investigate this kind of problem is dependency:tree.

Upvotes: 27

Bozhidar Batsov
Bozhidar Batsov

Reputation: 56665

If this dependency A has a compile scope - sure, it should have been downloaded and more over - made available in the classpath of the project. But if it had provided scope that would be the case, since provided deps would not be packaged with the application by Maven.

Btw how are you running this project - not running in the proper way might cause such problems as well and this is a very good guess. For example - if you're using maven exec plugin - maven will setup properly the classpath for you, but otherwise - you should setup it yourself(or build a jar with dependencies with the assembly plugin).

Upvotes: 4

Related Questions