A good friend has spent his lunch time to create a new page header for my new site. You can already see it - I hope you like it.


How do you like JDK6 ?
Home arrow Java Code arrow Java arrow Pack200 ANT task
Pack200 ANT task PDF Print
Written by Matthias Mann   

Ever tried to automate Pack200 creation of multiple JAR files ?

This ANT task allows you to use a <fileset> to specify the JAR files which needs to be (re)packed. That means it can process all JAR files of your project with only one task execution in your build.xml

Ok, Let's start with an example build.xml file that shows how to use it (taken from Worldscape):
<taskdef name="p200ant"
<target name="-post-jar">
    <input message="keyPass" addproperty="keypass"/>
    <input message="storePass" addproperty="storepass"
    <jar destfile="${dist.dir}/lwjgl-win32.jar"
        basedir="" includes="lwjgl.dll"/>
    <jar destfile="${dist.dir}/lwjgl-linux.jar"
        basedir="" includes=""/>
    <jar destfile="${dist.dir}/lwjgl-mac.jar"
        basedir="" includes="liblwjgl.jnilib"/>
    <fileset id="jars2pack" dir="${dist.dir}">
        <include name="**/*.jar"/>
        <exclude name="lwjgl-*.jar"/>
    <p200ant repack="true">
        <fileset refid="jars2pack"/>
    <signjar alias="MatthiasMann" keypass="${keypass}" storepass="${storepass}">
        <fileset dir="${dist.dir}">
            <include name="*.jar"/>
            <include name="lib/*.jar"/>
    <p200ant destdir="${dist.dir}">
        <fileset refid="jars2pack"/>

As you can see - nothing is hardcoded except the native libs for LWJGL. This allows to add JARs to the projects without the need to modify the ANT task.

Ok lets see what attributes this ANT task supports:

Attribute Description Required Default
repack Set to "true" to apply repacking to the JAR files No "false"
pack Set to "true" to write a uncompressed .pack file (not for repacking) No "false"
gzip Set to "true" to write a compressed .pack.gz file (not for repacking) No "true"
lzma Set to "true" to write a compressed .pack.lzma file (not for repacking, requires JLzma 4.65 library on the class path) No "false"
srcfile Specifies a single JAR file to (re)pack Unless <fileset> elements are used Not set
destfile When repack is set to "false" this can be used to place all .pack* files into the specified directory instead of the individual source directory of the JAR No Not set
keepOrder Keep the order of the files in the .pack.gz archive No "false"
KeepModificationTime Keep the modification time of individual files from the JAR file No "false"
singleSegment To create only a single Pack200 segment No "false"
segmentLimit Set the Pack200 segment limit in bytes No 1000000
configFile Reads Pack200 properties from a properties file No Not set

It is also possible to specify one or more <fileset> elements. In this case the srcfile attribute must not be set.

Now I think most of you are waiting for the sourcecode ?

Ok here it is: Source and Compiled JAR (published under the BSD license)

The LZMA SDK 4.65 is required to write LZMA compressed output files. The minimum Java heap size required is 256 MB

If you want to use Pack200 on an Apache 2.0 webserver you could use the following .htaccess file:
#Options MultiViews  # need only on some server, crashes at others
MultiviewsMatch Any
AddEncoding gzip .gz
AddEncoding pack200-gzip .pack.gz
AddType application/x-java-archive .jar

That's all - it will tell Apache that all files that have .jar as first extension are of the type "application/x-java-archive". And all files with the extension .gz have content-encoding of "gzip". But an extension .pack.gz has "pack200-gzip" as content encoding.
And the MultiViews options allows delegation of other files which are smaller and also satisfy the request made by the browser (Webstart in this case).

PS: There is also a command line interface to use it outside of ANT.

Add NewSearch
Only registered users can write comments!
alexmsk Unregistered | 2010-10-22 18:50:43
Here are my wishes for pack200 ant task:

1. `stripdebug' parameter

2. multithreading support (packing files in parallel on multi-core CPUs is much faster!)
Tom - Apache & Pack200 & JRE 1.4 Unregistered | 2010-10-20 12:08:53
I know, that your blog is not the perfect place to discuss my problem. But I did not find an adequate blog, forum, etc. Maybe someone can point me to a more suitable "place". My Problem is as follows:
To speed up our applet based application we are using the "pack200" compression for the corresponding jar-files (e.g., First.jar, see example below). The applet is hosted on Apache 2. Unfortunately we have to support "old" JREs such as JRE 1.4.2. Therefore, as pack200 is not supportet in Java 1.4, the un-compressed "version" of the jar-file should be delivered if requested by a browser with Java plugin ver. 1.4.2.

I did not manage it to configure our Apache according to these requirements.

Using the following configuration always (both, for JRE 1.4 and for JRE 1.6) delivers the "unpacked" (uncompressed) jar (i.e., First.jar):


AddType application/x-java-archive .jar
AddHandler type-map var
Options +MultiViews
MultiviewsMatch Handlers

AddEncoding pack200-gzip .jar
RemoveEncoding .gz

a) First.jar.var
URI: First.jar

URI: packed/First.jar.pack.gz
Content-Type: x-java-archive
Content-Encoding: pack200-gzip

URI: unpacked/First.jar
Content-Type: x-java-archive

Using instead configuration b) (see below), delivers the packed (compressed) jar-file for both, JRE 1.4 and JRE 1.6:

URI: First.jar

URI: packed/First.jar.pack.gz
Content-Type: x-java-archive
Content-Encoding: pack200-gzip

What I'm doing wrong? Anybody an idea?

I really appreciate your help, Tom
Paulo Eduardo Neves - Thanks! What about not repacki Unregistered | 2010-07-22 21:32:34
Thanks for the nice task. It is a lot better than the pack200 jar of Sun.

Is it possible to test if the jar is already repacked and don't repeat the operation? It would be a lot faster to repeat the operation. I'd be able to have my jars already packed and signed in the repository.
Tony Morris - pack200 Unregistered | 2010-06-09 04:00:22
You specify that there is a destfile attribute yet the source/jar have no such attribute. How does one specify the source and destination file to compress?
Frank Unregistered | 2009-07-23 17:13:22
Very useful, and very well documented. Thanks.
Francis Lalonde - using p200ant with maven Unregistered | 2008-06-30 21:15:22
oops, that pom.xml excerpt didnt go through very well. sorry for polluting the comments page. anyway, the trick to using p200ant with the antrun maven plugin is to install the jar file in the m2 repo, and then add a "dependency" section to the plugin definition block, referencing the p200ant jar.
David - Nice tool Unregistered | 2008-04-29 02:25:00
Am using it in my every jar packaging. And the ant task is really helpful..good work!
kappa - Useful Stuff Unregistered | 2008-04-14 02:01:12
Thanks for this most useful stuff.
Anonymous Unregistered | 2007-10-30 21:35:27
nice :), tnx you save a lot of typing to my poor fingers

Will Tatam Unregistered | 2007-02-20 15:35:31
Any chance of including the ant build file in the source so we can easily rebuild ?
Todd Unregistered | 2007-01-17 01:56:45

This is an extremely useful contribution to open source and I'd like to start by thanking you for it.

But I also have a request. It appears that the compiled jar was made with JDK 1.6 and that keeps it from being loaded properly in a 1.5 environment. I worked around the problem by simply recompiling the provided source. However, if you could supply the jar compiled with 1.5, that would be a nice benefit to others.
Thanks again.
Last Updated ( Thursday, 31 December 2009 )
< Prev   Next >
2016 Matthias Mann,
Joomla! is Free Software released under the GNU/GPL License.