Launch4J - how to attach dependent jars to generated exe

前端 未结 7 750
深忆病人
深忆病人 2020-11-28 13:43

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 structur

相关标签:
7条回答
  • 2020-11-28 14:15

    To include external libraries with Launch4j you have to have the ".jar" files of the external libraries near your .exe (mine are just in the same folder) then in "Classpath" you put in the path to the .jar files into "Edit Item: "

    0 讨论(0)
  • 2020-11-28 14:15

    In launch4j go to the classpath tab. Tick the custom classpath field. In the edit field, enter the full path of each jar you want included and press accept. When finished type just the name of the main class into the separate field (ie MyProg). All the jars will now be included in the exe.

    PS I have all the jars in the same directory. I'm using version 3.12

    0 讨论(0)
  • 2020-11-28 14:21

    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 :-)

    0 讨论(0)
  • 2020-11-28 14:22

    Putting different links of places which had helped me

    How to include all dependent Jars within a single non-executable jar?

    How can I create an executable JAR with dependencies using Maven?

    http://www.mkyong.com/java/how-to-make-an-executable-jar-file/

    and most importantly

    http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html

    quick tut

    http://www.mkyong.com/java/how-to-add-your-manifest-into-a-jar-file/

    0 讨论(0)
  • 2020-11-28 14:29

    When you are converting your .jar file

    1. Go to classpath tab
    2. Check custom classpath
    3. On main class select your .jar from your dist folder after building the project
    4. On the classpath textarea add your libraries, you add them right below that textarea writting the full path to the lib (of course including the lib, ie "C:\folder\lib\file.jar")

    0 讨论(0)
  • 2020-11-28 14:31

    I have spent hours on this issue. So here is my contribution.

    The problem here: how to sucessfully put your external jars that your .JAR program needs INSIDE the .exe that you generate.

    We assume that you already, and correctly, configured the external jars on eclipse/netbeans and ALL WORK FINE with the command: java -jar yourprogram.jar.

    So the real problem is how to ensure that this .EXE file will contain the external jars, otherwise it will not work properly.

    1) First of all, you need to forget Launch4J and anyother program.

    2) Install JSmooth, I recommend that you use the windows version.

    3) On the left menu there is a button "Application". Click on it.

    4) You will see a tab "Classpath" panel. Click on the plus (+) and add your external .jar's files. And that is it!!

    Don't forget to put your .jar application marking checkbox "use am embedded jar" and choose the main class properly. It will work.

    0 讨论(0)
提交回复
热议问题