Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimizing jar dependency sizes

an application I have written uses several third party jars. Sometimes only a small portion of the entire 50kB to 1.7mB jar is used - one or two function calls or classes.

What is the best way to reduce the jar sizes. Should I download the sources and build a jar with just the classes I need? What existing tools can help automate this (ex I briefly looked at http://code.google.com/p/jarjar/)?

Thank you

Edit 1: I would like to lower the size of my third party 'official' jars like swingx-1.6.jar (1.4 MB), set-3.6 (1.7 MB) glazedlists-1.8.jar (820kB) , etc. so that they only contain the bare minimum classes I need

Edit 2: Minimizing a jar by hand or by using a program like proguard is further complicated if the library uses reflection. Injection with google guice does not work anymore after obfuscation with proguard

The answer by cletus on another post is very good How to determine which classes are used by a Java program?

like image 625
brian_d Avatar asked Sep 28 '10 23:09

brian_d


People also ask

How can I reduce the size of a jar file?

A jar (and war and ear) are . zip archives that have a specific directory and file structure under them. As they are zipped, the only way to further compress the contents is to remove things from them.

Can we compress jar file?

HTTP Compression allows applications JAR files to be deployed as compressed JAR files. The supported compression techniques are gzip, compress, and deflate. As of SDK/JRE version 5.0, HTTP compression is implemented in Java Web Start and Java Plug-in in compliance with RFC 2616.

Is JAR compressed?

Items stored in JAR files are compressed with the standard ZIP file compression. Compression makes downloading classes over a network much faster. A quick survey of the standard Java distribution shows that a typical class file shrinks by about 40 percent when it is compressed.

How do I reduce the size of a class in Java?

Use an obfuscator like ProGuard, JoGa, or JShrink to optimize the size of your class. Use a single character for the class file name. This reduces its size internally, reduces the amount of info the Zip program stores, and reduces the size of the manifest. Reference as few classes as possible.


1 Answers

Proguard would be an option. It can eliminate unused classes and methods. You can also use it to obfuscate, which can further reduce the size of your final jar. Be aware that class loading by name is liable to break unless care is taken to keep the affected classes unobfuscated.

I've found Proguard quite effective - can be a bit cryptic to understand at the outset. But I don't have any experience with similar to offer a comparison.

like image 54
martin clayton Avatar answered Sep 20 '22 04:09

martin clayton