9

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.

Rick
  • 203
  • 1
  • 2
  • 7
  • 2
    [Sharing your research helps everyone](http://meta.programmers.stackexchange.com/questions/6559/why-is-research-important). 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 [ask] – gnat Feb 09 '16 at 08: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 09 '16 at 09:00
  • Why did the college give you an assignment that's too big to upload? – Robert Harvey Feb 09 '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 09 '16 at 14:46
  • It was a rhetorical question. – Robert Harvey Feb 09 '16 at 14:48
  • "I can't remove the images" ... but can you reduce their size by compressing the image files differently? maybe reduce their resolution and/or color depth first? – Kjetil S. Jan 10 '19 at 16:37
  • You can reduce the size of the .class files already. Having less to compress will surely help lowering the jar size. E.g. do not include debug info with `javac -g:none`. See https://stackoverflow.com/questions/5257987/any-java-counterpart-for-usr-bin-strip – cachius Jul 01 '23 at 10:23

3 Answers3

8

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.

msell
  • 576
  • 3
  • 6
  • How would either of these reduce the jar size with images in the resources? –  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 '19 at 19:42
3

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.

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.