Search code examples
javamavendependenciesclassloaderdependency-management

How to handle conflicting dependencies in Java


In my Java project I have two external dependencies, A and B. I have these as JAR files on my build path. Both project A and B have the external dependency project C. However they rely on different versions of C. If I use the version that's compatible with A, then B breaks, and vice versa.

How to handle this?


Solution

  • Welcome to dependency hell.

    Three approaches:

    1. If at least one of the projects A and B is yours, you can try to change the code to reach compatibility with the desired version of C. Ideally, you can update both A and B to the latest stable version of C and "repair" the code to make it work.

    2. If both A and B are external projects, you might experiment with different versions of A and B because they probably rely on different versions of C. Maybe you'll find a combination that works with the same C.

    3. Construct a shaded jar with the Maven shade plugin. Here, you modify the package names of one version of C to make it independent of the other version.