Launch4J – how to attach dependent jars to generated exe

As it often happens being unable to solve the problem I published it on StackOverflow … and pretty soon after publishing the question I got an idea.

So the answer to my question is:

Put all the dependent jars into one main jar.

It took me some time to find info how can I do that.

To help people I decided to publish detailed instruction here – they are based on Netbeans 7.4.

  1. Following article from http://mavistechchannel.wordpress.com/2010/08/17/how-to-build-a-single-jar-file-with-external-libs/ I created the ant script that build one-jar-app for me.
    I could then manually create exe via Launch4J

  2. I then decided that I want more automated task, and I did that, Ant builds exe for me (via Launch4J)

  3. Then I realized that I must do “clean and build” before my automated task (in point 2)/ I decided that I want clean and build to be done automatically before the exe build

Putting all together I am attaching my ant build script consisting of points 1,2,3:

It is required to edit build.xml and put the content found below before “project” end tag

<target name="package-for-launch4j" depends="clean,compile,jar">
    <property name="launch4jexe.dir" location="C:\Program Files (x86)\Launch4j" />
    <taskdef name="launch4j"
             classname="net.sf.launch4j.ant.Launch4jTask"
             classpath="${launch4jexe.dir}/launch4j.jar
            :${launch4jexe.dir}/lib/xstream.jar" />
    <property name="launch4j.jar.name" value="MyAppJarName"/>
    <property name="launch4j.dir" value="exe"/>
    <property name="launch4j.jar" value="${launch4j.dir}/${launch4j.jar.name}.jar"/>
    <echo message="Packaging ${application.title} into a single JAR at ${launch4j.jar}"/>
    <delete dir="${launch4j.dir}"/>
    <mkdir dir="${launch4j.dir}"/>
    <jar destfile="https://stackoverflow.com/questions/19917960/${launch4j.dir}/temp_final.jar" filesetmanifest="skip">
        <zipgroupfileset dir="dist" includes="*.jar"/>
        <zipgroupfileset dir="dist/lib" includes="*.jar"/>
        <manifest>
        <attribute name="Main-Class" value="${main.class}"/>
        </manifest>
    </jar>
    <zip destfile="${launch4j.jar}">
        <zipfileset src="https://stackoverflow.com/questions/19917960/${launch4j.dir}/temp_final.jar"
            excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
    </zip>
    <delete file="https://stackoverflow.com/questions/19917960/${launch4j.dir}/temp_final.jar"/>
    <launch4j configFile="misc/l4j-myapp.xml" />
</target>

then in Netbeans rightclick on the build.xml and choose:
Run Target / Other Targets / package-for-launch4j

exe file is ready in exe folder 🙂

Leave a Comment