确定使用的库以减少 JAR 文件大小

发布于 2024-10-08 13:56:22 字数 558 浏览 0 评论 0原文

我在某些程序中使用 HtmlUnit,但总是遇到这样的问题:每当我使用它时,我都必须添加从 HtmlUnit 网站下载的所有文件(库),因此我获得的 jar 文件始终为 10 mb。

这些是我添加的文件:

alt text

但在我的大多数程序中我只包含这个:

import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;

我通常只使用 WebClient和 HtmlElements(HtmlForm、HtmlTable 等),所以我觉得很奇怪我需要所有这些。

我的问题是,我是否可以减少包含的库的数量,或者更好,如何确定我包含的哪些库根本没有被使用。

I am using HtmlUnit in some programs and I always have the problem that whenever I use it, I have to add all the files (libraries) that I downloaded from HtmlUnit website so the jar file that I get is always 10 mb.

These are the files that I add:

alt text

But in most of my programs I only include this:

import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.RefreshHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;

I usually only use WebClient and HtmlElements (HtmlForm, HtmlTable, etc.), so i find it strange that I need all of them.

My question is if I could reduce the number of libraries included, or maybe better, how can I determine which libraries that I include are not being used at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

今天小雨转甜 2024-10-15 13:56:22

您是否真的想检查所有添加的库的所有类是否需要?

恕我直言,这是程序的工作。这样做的一个流行的开源程序是 ProGuard

一般来说,ProGuard 被称为混淆器(重命名类文件中的所有内容,因此很难理解反编译的类),但在使用参数 -dontobfuscate 时,可以在不混淆的情况下使用它。另外,我建议您也使用-dontoptimize参数。

如果你在你的大 JAR 上运行 ProGuard,你会得到一个小得多的 JAR。

如果您有兴趣,我可以发布 ant 构建文件的一小部分,展示如何配置和调用 ProGuard。

Do you really want to check all classes of all added libraries if they are needed or not?

IMHO this is a job for program. One popular open source program for doing so is ProGuard.

Generally ProGuard is known as obfuscator (rename everything in the class files so that understanding the decompiled classes is difficult) but it can be used without obfuscation when using the parameters -dontobfuscate. Additionally I suggest you to use the -dontoptimize parameter as well.

If you run ProGuard on you big fat JAR you will get a much smaller one.

If you are interested I could post a small part of an ant build file that shows how to configure and call ProGuard.

猛虎独行 2024-10-15 13:56:22

您不仅必须考虑直接使用的类,还要考虑它们的所有依赖项。您必须追踪整个对象图才能找出未调用的内容。

You have to take into consideration not only the class you use directly, but all their dependencies as well. You'll have to chase down the entire object graph to figure out what's not called.

盛装女皇 2024-10-15 13:56:22

我知道 JBoss Tattletale,但我自己还没有尝试过。

请记住,库可能仅在代码的某些运行期间被访问(例如,取决于输入),因此很难确定是否需要库。

如果您删除了某些库,您的应用程序现在可能可以运行,但在不同的输入上会失败,或者在下一个版本中开始以不同方式使用 HtmlUnit 时会失败。如今 10 MB 已经不算多了,所以也许删除一些 jar 并不值得这么麻烦。

I know about JBoss Tattletale, but I haven't tried it myself.

Keep in mind that a library might be accessed only during certain runs of the code (e.g. depending on the input), so it's very difficult to be certain whether a library might be needed.

If you get rid of some of the libraries, your application may work now, but fail on a different input, or fail when you start using HtmlUnit differently in your next release. 10 MB isn't so much nowadays, so maybe getting rid of some jars is not worth the trouble.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文