Search code examples
javajarlaunch4j

Launch4J - how to attach dependent jars to generated exe


I have a simple java project, which requires external jars. I build this with netbeans and after Clean and Build command, I can find in dist directory the following structure:

-myApp.jar
-lib/
     library1.jar
     library2.jar

typical, I would say.

Now, I'd like to distribute myApp.jar with dependent libraries as one exe. Is this possible? I am trying to use Launch4J. In the GUI I create the config file, there are some options in cp section

<cp>lib/swing-layout-1.0.4.jar</cp>

but it seems to be classpath, and it is the only place I can refer to my extra jars.

After exe file is created, I can't find dependend libs in the exe (exe can be opened with winrar) and thus my application crashes.

How can I make the exe file properly then?

Thanks for your help.


Solution

  • 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="${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="${launch4j.dir}/temp_final.jar"
                excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
        </zip>
        <delete file="${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 :-)