java 编译java项目时如何找出实际使用的jar文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1763710/
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-29 17:52:20  来源:igfitidea点击:

How do I find out what jar files are actually used when compiling a java project

javajarjavac

提问by tomdee

I'm currently passing a very large classpath to javac to compile a java project.

我目前正在将一个非常大的类路径传递给 javac 来编译一个 java 项目。

I know that a number of those jar files aren't needed.

我知道其中一些 jar 文件是不需要的。

Is there a simple way of finding out which files aren't needed?

有没有一种简单的方法可以找出不需要的文件?

采纳答案by sanity

You need the Class Dependency Analyzertool. To quote the introduction:

您需要类依赖分析器工具。引用介绍:

The purpose of this tool is to analyze Java? class files in order to learn more about the dependencies between those classes.

这个工具的目的是分析Java?class 文件,以便了解有关这些类之间的依赖关系的更多信息。

True, it won't catch runtime dependencies - but short of running an exhaustive 100% coverage test suite you can never be sure you've caught all runtime dependencies.

确实,它不会捕获运行时依赖项 - 但如果没有运行详尽的 100% 覆盖率测试套件,您永远无法确定您已捕获所有运行时依赖项。

If you expect runtime dependencies you should use the CDA as a first-pass, then do exhaustive testing of the resultant app to ensure that there are no jar files which were only referenced through runtime dependencies.

如果您期望运行时依赖项,则应首先使用 CDA,然后对生成的应用程序进行详尽测试,以确保没有仅通过运行时依赖项引用的 jar 文件。

回答by Pascal Thivent

I guess that "remove them one by one and check if the application still compiles and works" is not the expected answer :)

我猜“一个一个地删除它们并检查应用程序是否仍然可以编译和工作”不是预期的答案:)



(EDIT: While the approach suggested above can be a bit automated, it remains somehow painfull and there must be an alternative, at least for compile-time dependencies. After some googling, I found Jar Analyzerwhich seems to be a nice tool for this work as explained in this blog post:

(编辑:虽然上面建议的方法可能有点自动化,但它仍然有些痛苦,并且必须有替代方案,至少对于编译时依赖项。经过一些谷歌搜索,我发现Jar Analyzer这似乎是一个很好的工具按照这篇文中的解释工作:

Jar Analyzer scans for compile dependencies, meaning that it can create a tree of which JAR-files are needed to compile which JAR-files that are needed to compile these JAR-files, and so on. You get a very nice report/graph which shows you all the JAR-files and why they are in there.

You can also see the JAR-files that don't have any connection to your code, remove them and their children. What I found in our libs folder was that about 20% of the 150 JAR files in our libs folder were unused at compile time, and these were potential JARs to be removed.

The big aber is that you don't get any hint on which JAR-files are used only at runtime by means of discovery and reflection. And this is where the real work begins.

The only way to find out whether a JAR file is used at runtime is basically to take it out, start up your application and test every functionality. If you have an application of moderate size, performing a 100% regression test takes many hours. So in practice, I ended up doing alot of guessing, quick and dirty testing, and asking around to find out which of the runtime dependencies were actually in use.

Jar Analyzer 扫描编译依赖项,这意味着它可以创建一个树,其中包含需要哪些 JAR 文件来编译哪些 JAR 文件需要哪些 JAR 文件来编译这些 JAR 文件,依此类推。你会得到一个非常好的报告/图表,它向你展示了所有的 JAR 文件以及它们为什么在那里。

您还可以查看与您的代码没有任何联系的 JAR 文件,删除它们及其子文件。我在 libs 文件夹中发现,在编译时,libs 文件夹中的 150 个 JAR 文件中约有 20% 未使用,这些是可能要删除的 JAR。

最大的错误是您无法通过发现和反射获得任何关于仅在运行时使用哪些 JAR 文件的提示。这就是真正的工作开始的地方。

确定是否在运行时使用 JAR 文件的唯一方法基本上是将其取出,启动您的应用程序并测试每个功能。如果您有一个中等大小的应用程序,执行 100% 回归测试需要很多小时。所以在实践中,我最终做了很多猜测、快速和肮脏的测试,并四处打听以找出实际使用了哪些运行时依赖项。

It seems pretty easy to use: download, unzip and run the tool on a directory containing all jars. Or use the provided Ant task.)

它看起来很容易使用:下载、解压缩并在包含所有 jar 的目录上运行该工具。或者使用提供的 Ant 任务。)

回答by Anthony O.

You also have loosejar.jarwhich enables you to find the real jar dependencies of your project at runtime !

您还拥有loosejar.jar,它使您能够在运行时找到项目的真正jar 依赖项!

回答by JERiv

The latest build of eclipse will warn you about unused imports in your source code

最新版本的 Eclipse 将警告您源代码中未使用的导入

回答by notnoop

The compiler has a -verboseoption, and it is quite verbose. It informs of each class that gets loaded and where it is loaded from!

编译器有一个-verbose选项,它非常冗长。它通知每个被加载的类以及它是从哪里加载的!

While it's not quite user-friendly and it doesn't provide high-level analysis, I found it very useful for debugging classpath conflicts. This tells you the jars that get used (with help of grep), not the ones that don't get used.

虽然它对用户不是很友好,也没有提供高级分析,但我发现它对于调试类路径冲突非常有用。这会告诉您已使用的 jar(在 的帮助下grep),而不是未使用的 jar 。

回答by JuanZe