36

I have a Java application built with Maven with a lot of dependencies. When performing my test cases they sometimes pass fine, sometimes they fail because of some incompatible class combinations. So it seems to that there must be some classes twice in classpath which are taken randomly. The one is fine the other not.

  • How can I find out which classes / jars are incompatible in my classpath?
  • What is the right approach using Maven not to fall in that compatibility-traps?
Jagger
  • 10,350
  • 9
  • 51
  • 93
mibutec
  • 2,929
  • 6
  • 27
  • 39
  • How can Maven know? OSGi is the answer.... – duffymo Sep 21 '12 at 18:53
  • The standalone tool [Tattletale](http://www.jboss.org/tattletale) is a good choice, personally I use [progurad](http://proguard.sourceforge.net/) as one-stop solution, as it is more easy to integrate with Maven. – yorkw Sep 22 '12 at 03:13

7 Answers7

54

I think a better solution would be to use the maven-duplicate-finder-plugin.

Note: The new version is the duplicate-finder-maven-plugin.

sleske
  • 81,358
  • 34
  • 189
  • 227
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • 14
    This worked like a charm for me! No setup, no editing `pom.xml`, simply calling `mvn com.ning.maven.plugins:maven-duplicate-finder-plugin:1.0.4:check` from the command line. – Joachim Sauer Aug 26 '13 at 15:35
  • 6
    Is there a Gradle equivalent to this plugin? – Renato Mar 07 '16 at 10:56
  • 2
    FYI there is something similar here: http://www.mojohaus.org/extra-enforcer-rules/banDuplicateClasses.html – vorburger Jan 23 '17 at 20:34
  • 4
    The command line for the latest version is `mvn org.basepom.maven:duplicate-finder-maven-plugin:1.4.0:check` – Pino Dec 17 '19 at 09:58
  • @Renato No equivalent exists. I prepared my own gradle plugin extending it from https://github.com/tehlers/gradle-duplicate-classes-check/blob/master/src/main/groovy/net/idlestate/gradle/duplicates/CheckDuplicateClassesTask.groovy with notable differences: 1) only inspect runtimeClasspath dependencies 2) inspect also current project source files – Alex Sep 03 '20 at 15:17
  • @JoachimSauer it searches in 3rd party jars as well! how to avoid that? – Gaurav Aug 11 '22 at 06:54
17

You can try using this tool Tattletale.

Jagger
  • 10,350
  • 9
  • 51
  • 93
  • 4
    the tool seems to be dead, last release was on 17.2.2012, see: https://issues.jboss.org/browse/TTALE/fixforversion/12318287 – Peter Butkovic Dec 05 '13 at 08:21
  • 1
    Though this tool is dead, this gives a pretty nice results, which are far better than other tools. – Baskar Mar 26 '15 at 20:40
  • 1
    Hint: to make Tattletale work on jars compiled with Java 8, replace javassist.jar with a newer version, for example https://mvnrepository.com/artifact/org.javassist/javassist/3.18.2-GA. Also, the best report I found for the discussed purpose is called "Multiple Jar Files". – Speedstone Jan 10 '21 at 19:52
5

You can detect duplicate classfile definitions in the classpath or module path using ClassGraph (disclaimer, I am the author of ClassGraph):

for (Entry<String, ResourceList> dup :
        new ClassGraph().scan().getAllResources().classFilesOnly().findDuplicatePaths()) {
    System.out.println(dup.getKey());              // Classfile path
    for (Resource res : dup.getValue()) {
        System.out.println(" -> " + res.getURI()); // Resource URI, showing classpath element
    }
}
Luke Hutchison
  • 8,186
  • 2
  • 45
  • 40
  • Is there is any dependency POM file which i should import to run this piece of Snippet ? ` io.github.classgraph classgraph X.Y.Z ` – Sam Berchmans Jul 30 '20 at 14:10
  • @SamBerchmans yes, the Maven instructions are on the ClassGraph project page: https://github.com/classgraph/classgraph#downloading There is extensive documentation here: https://github.com/classgraph/classgraph/wiki And there are other code examples here: https://github.com/classgraph/classgraph/wiki/Code-examples – Luke Hutchison Jul 30 '20 at 23:26
2

There is a plugin in eclipse to check for duplicate classes in the build path (ClasspathChecker http://classpathchecker.free.fr/)

mic.sca
  • 1,688
  • 2
  • 18
  • 34
2

This problem is basically an application of the more general problem to "somehow scan the classpath (CP) and collect all class files and other resources", and then find duplicates in that...

There are a number of existing libraries for CP scanning (and it's not trivial to do this right in all environments, especially since the application class loader in Java 9 is no longer an URLClassLoader), notably Classgraph, using which it's relatively trivial to do this.

PS: For Java versions <9, JHades (jhades.github.io) is nice (but NOK on Java 9/10/11).

vorburger
  • 3,439
  • 32
  • 38
1

This is a another simple Open Source Duplicate Classpath Finder tool - Classpath Inspector

which gives pretty decent report of duplicate classes in the classpath.

Baskar
  • 1,439
  • 12
  • 17
-3

You can use the maven dependency:tree to see the maven hierarchy of your project and maven exclusion to exclude the jars you don't want

Makis Arvanitis
  • 1,175
  • 1
  • 11
  • 18