简体   繁体   中英

Find duplicated classes in classpath

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?

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

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

您可以尝试使用此工具Tattletale

eclipse 中有一个插件可以检查构建路径中的重复类(ClasspathChecker http://classpathchecker.free.fr/

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

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

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

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

您可以使用 maven dependency:tree 查看项目的 maven 层次结构,并使用maven 排除来排除您不想要的 jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM