Question

I dont know how to reduce the size of jar file. When we normally code in Java Swing the jar file is created, is there any way to reduce the size of jar file? I can't remove the images and other stuff which I have included as it's necessary.

I tried to unzip the jar file and and tried to remove the stuff which is not needed. I wanted to reduce jar file size because I have to upload it in a portal for my college project, max size to upload is 1 MB.

Was it helpful?

Solution

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. This may be in the form of deduplication (I once dealt with deployment that had duplicates for the classes and the client that were packaged in the same deployment - though admittedly that was deployed in an exploded structure) or it may be in moving the resources to other services.

Images tend to be already compressed and thus themselves compress poorly in a compressed archive (such as a .jar). If the images are making the .jar too large, you need to remove them or replace them with smaller images (change the complexity of the image to something that compresses better).

In the case of "I wanted to reduce jar file size because I have to upload it in a portal for my college project" your recourse is probably to talk to the people giving you the assignment. If you can't make the images smaller or remove them from the .jar, you can't make the .jar smaller.

OTHER TIPS

There are actually multiple ways to reduce the size of .jar files. If you really need to reduce the size of .jar file without dropping some content, I would recommend trying the following two automated tools.

Compression

Perhaps the easiest way is to use a different tool to create the .jar file than the standard jar distributed with the Java SDK. KZIP is one such tool and the best one I have encountered in terms of compression efficiency.

Obfuscators

Obfuscators such as Proguard can often reduce the size of jar files considerably. Besides making the application harder to reverse engineer they can typically manipulate the byte code in a way that makes it smaller without affecting the functionality. This includes but is not limited to removing unused code and renaming symbols to shorter ones.

There are also more ways to reduce the jar size (e.g. using uncompressed png images for better jar compression), but they are not as practical to use.

Well there are ways to reduce the size of jar file among which one is to optimally apply depencices to your project object manager (pom.xml or gradle.build)

  1. Use only required dependencies.
  2. IF you have test dependencies put those under test scope.
Licensed under: CC-BY-SA with attribution
scroll top