Viswanath Alikonda
Viswanath Alikonda

Reputation: 137

How to reduce jar size

I am including a library of 49MB size. But, I use only few features and I don't want the entire library to be present in the final jar. I used proguard. But, I was not successful in cutting down the size.

Can anyone pls let me know the correct tool, that can remove unused classes/jars intelligently.

Upvotes: 3

Views: 6045

Answers (2)

sanastasiadis
sanastasiadis

Reputation: 1202

If you have a Maven project, you can use the maven-shade-plugin, that has a configuration called minimizeJar.

You can bind the minimizeJar configuration option of maven-shade-plugin with the package phase of your application by:

<project>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                        <minimizeJar>true</minimizeJar>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    ...
</project>

Reference: Selecting contents for Uber jar

Upvotes: 4

Felipe Martinez
Felipe Martinez

Reputation: 53

Java SE 5 and 6 comes already with a packing tool called "pack200". This tool is able to compress already existing jar files which can be then used for network distribution. I compressed the rt.jar with the following command:

C:\Programme\Java\jdk1.6.0\jre\lib>pack200 -J-Xmx256m rt.jar.gz rt.jar

The flag -J-X.. is needed, because otherwise OutOfMemory exception can occur (pack200 is written in Java...) The results are amazing:

Origin size: 43.8 MB Compressed size: 5.81 MB

For unpacking the tool "unpack200" can be used.

The tool pack200 is especially interesting for the distribution of WebStart applications to achieve faster download times. There is already one another well known sample - glassfish. In the second installation stage, glassfish unpacks internal libraries using the unpack200 tool...

font: http://www.adam-bien.com/roller/abien/entry/how_to_reduce_the_jar

Upvotes: 1

Related Questions