Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce external jar file size

Tags:

java

jar

size

I've developed a module for a Java project. The module depends on external library (fastutil). The problem is, the fastutil.jar file is a couple of times heavier than the whole project itself (14 MB). I only use a tiny subset of the classes from the library. The module is now finished, and no-one is likely to extend it in future. Is there a way I could extract only the relevant class to some fastutil_small.jar so that others don't have to download all this extra weight?

like image 442
joe_shmoe Avatar asked Apr 09 '10 09:04

joe_shmoe


People also ask

How do I reduce the size of a .JAR file?

Obfuscation typically reduces jar file size by a respectable factor. You may want to try tools like Proguard (open source) and similar. You can see some examples of size reduction in this page: http://proguard.sourceforge.net/index.html#/results.html.

Can you compress a JAR file?

Compressing files in JAR format: We use the JarArchiveOutputStream to compress files and/or directories into JAR format. We can add entries in the archive using the JarArchiveOutputStream. putArchiveEntry method and pass in a JarArchiveEntry as an argument containing the file and filename respectively..

Can you modify a JAR file?

You can use the JAR file editor to view or edit JAR file information that is stored in a data development project. If you make changes to JAR file information using the editor, you must deploy the JAR file to the database server again to apply the changes to the server version of the JAR file.


1 Answers

Obfuscation tools such as ProGuard usually provide a feature to remove unused classes (and even fields and methods) from the jar file. You have to be careful to verify everything still works, 'though, because you might be using reflecton to access classes or methods that ProGuard can't analyze.

You can use only that feature and already get quite some saving

Or you could combine it with other space-saving obfuscation techniques (such as class and method renaming) to save even more space at the cost of harder debugging (your stack traces will become harder to parse).

like image 67
Joachim Sauer Avatar answered Oct 12 '22 02:10

Joachim Sauer