Determine used libraries to reduce JAR file size

随声附和 提交于 2019-12-11 07:28:41

问题


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:

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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4460757/determine-used-libraries-to-reduce-jar-file-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!