java 在类路径中查找重复的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12536482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:17:58  来源:igfitidea点击:

Find duplicated classes in classpath

javamaven

提问by mibutec

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.

我有一个用 Maven 构建的 Java 应用程序,有很多依赖项。在执行我的测试用例时,它们有时会通过,有时会由于某些不兼容的类组合而失败。所以看起来类路径中一定有一些类是随机抽取的。一个很好,另一个不行。

  • 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?
  • 如何找出我的类路径中哪些类/罐子不兼容?
  • 使用 Maven 避免陷入兼容性陷阱的正确方法是什么?

回答by khmarbaise

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

我认为更好的解决方案是使用maven-duplicate-finder-plugin

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

注意:新版本是duplicate-finder-maven-plugin

回答by Jagger

You can try using this tool Tattletale.

您可以尝试使用此工具Tattletale

回答by mic.sca

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

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

回答by Baskar

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

这是另一个简单的开源重复类路径查找器工具 - Classpath Inspector

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

这给出了类路径中重复类的相当不错的报告。

回答by vorburger

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

这个问题基本上是更普遍的问题的应用,即“以某种方式扫描类路径(CP)并收集所有类文件和其他资源”,然后在其中找到重复项...

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.

许多用于 CP 扫描的现有库(并且在所有环境中正确执行此操作并非易事,特别是因为 Java 9 中的应用程序类加载器不再是 URLClassLoader),特别是Classgraph,使用它来执行此操作相对简单.

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

PS:对于 Java 版本 <9,JHades( jhades.github.io) 很好(但在 Java 9/10/11 上不可以)。

回答by Luke Hutchison

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

您可以使用ClassGraph检测类路径或模块路径中的重复类文件定义(免责声明,我是 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.getURL()); // Resource URL, showing classpath element
    }
}

回答by Makis Arvanitis

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

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