44

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?

isherwood
  • 58,414
  • 16
  • 114
  • 157
Jake
  • 15,007
  • 22
  • 70
  • 86

4 Answers4

27

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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    re the last sentence: The dependency hierarchy view in m2eclipse is a lot more user-friendly than dependency:tree, it even gives you an 'exclude' wizard – Sean Patrick Floyd May 31 '10 at 14:37
  • 1
    @seanizer: True (assuming the OP is using m2eclipse). Note that [`dependency:tree`](http://maven.apache.org/plugins/maven-dependency-plugin) also supports [filtering](http://maven.apache.org/plugins/maven-dependency-plugin/examples/filtering-the-dependency-tree.html). – Pascal Thivent May 31 '10 at 15:26
  • I'm assuming that anybody who uses maven and eclipse together also uses m2eclipse, as it's much more powerful and comfortable than the maven-eclipse-plugin (pom editor, repository index, automatic config changes when pom was edited etc). and of course it also includes filtering: click on any artifact in the hierarchy and only this artifact's hierarchy is shown... – Sean Patrick Floyd May 31 '10 at 15:55
  • 11
    @Jake: I know this question is old, but i have the same problem and don't see how this answer solved the problem - can you tell me how you solved it? Btw. a workaround is to add the transitive dependencies as direct dependencies, but i would like to have "the real" solution. – kutschkem Dec 08 '12 at 13:07
  • @kutschkem did You figure it out yet? After all those years :) – Thomas Nov 22 '16 at 23:12
  • @fane89 I can't remember if I found an actual solution, but an alternate approach to the workaround I mentioned is copying the dependencies to another directory and adding that to the classpath: http://stackoverflow.com/questions/7908090/downloading-all-maven-dependencies-to-a-directory-not-in-repository – kutschkem Apr 28 '17 at 14:00
  • 1
    @kutschkem in my case the problem was at the deployment phase. I was using `mvn deploy:deploy-file` to deploy my project to the deployment server. However, _by default_ this re-generates your `pom.xml` and destroys your project's list of dependencies! See solution here: https://stackoverflow.com/questions/58698453/maven-transitive-dependency-not-loaded – Jonathan Benn Oct 27 '22 at 17:40
3

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).

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
  • A lists B as a dependency with "compile" scope; P lists A as a dependency with "compile" scope. B is not being added to P's classpath. – Jake May 26 '10 at 17:27
  • You didn't answer the question of 'how are you running it?' – bmargulies May 26 '10 at 17:37
  • @bmargulies Pardon. I generate Eclipse metadata and run it out of the Eclipse debugger. – Jake May 26 '10 at 17:40
  • You generate it with mvn eclipse:eclipse? – Bozhidar Batsov May 26 '10 at 18:05
  • Yes, I generate the metadata with `mvn eclipse:clean eclipse:eclipse`. A's jar is then included in the Eclipse "Referenced Libraries" for P, but no dependency of A is included (and thus B is not included either). – Jake May 26 '10 at 19:45
0

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.

Emadpres
  • 3,466
  • 2
  • 29
  • 44
-3

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...

physox
  • 313
  • 5
  • 7