Java – How to Reduce JAR File Size

compilerjavajava-eeswing

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.

Best Answer

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.

Related Topic