2

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.

  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Feb 9 '16 at 8:34
  • 4
    Jar files are already compressed. Your only options are to find stuff to take out, or replace the images with ones that compress better. – Simon B Feb 9 '16 at 9:00
  • Why did the college give you an assignment that's too big to upload? – Robert Harvey Feb 9 '16 at 14:43
  • @RobertHarvey I have no idea max size to upload is 1 MB and my Jar size is 1065 KB – Rick Feb 9 '16 at 14:46
  • It was a rhetorical question. – Robert Harvey Feb 9 '16 at 14:48
2

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.

6

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.

  • How would either of these reduce the jar size with images in the resources? – user40980 Feb 10 '16 at 11:53
  • @MichaelT Better compression works also for images (maybe not as much). Also the question didn't say that the jar file contains only images, so it's safe to assume there are also Java class files in it. – msell Feb 10 '16 at 12:11
  • +1 if there is a lot of code, mostly through dependencies, proguard can help quite a lot. You could include just compiling without the -g flag (don't put debug infomration in the bytecode). Will have to be applied to all dependencies, though, and Proguard can remove that info, too. – marstato Mar 18 at 19:42
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.