How to combine two Jar files

后端 未结 8 1425
情深已故
情深已故 2020-11-28 05:31

Is it possible to combine two jar files such that in an applet tag I can simply do something like

archive=\"jarjar.jar/jar1.jar\"...  ...archive=\"jarjar.jar         


        
相关标签:
8条回答
  • 2020-11-28 05:57

    There is also the eclipse fatjar plugin.

    0 讨论(0)
  • 2020-11-28 05:59

    I know it's an old question and I just wanted to add my two cents (no permission to comment yet, so creating a new answer).

    I do see the value in sumanth.donthula's answer as the problem for all of us merging jars will be how to deal with the manifest files. In my case I wanted to merge some library files (mainly generated web service client code) into the jar of an application written by me. It was OK to replace the manifests with the one of my own jar.

    The simplest way of doing this is taking care of the order in which you unzip the original files (as Alnitak and sumanth.donthula noted).

    I wanted to use the zip ant task (thank you, ykombinator, for the idea). It turned out that the only way of controlling the order of compressing/packaging is renaming the files. See my ant target below.

    The output directory in my example is called codemodule.dir (I created a FileNet code module). The rest of the names are self-explaining. The important step is renaming the application jar to 0_... to be the 1st in order. This way its manifest will be retained as the duplicate attribute of the zip ant task is set to preserve.

    <target name="merge_jars">
        <delete dir="${codemodule.dir}" quiet="true" />
        <mkdir dir="${codemodule.dir}" />
        <copy todir="${codemodule.dir}">
            <fileset dir="${lib.dir}" includes="*.jar"/>
            <fileset dir="${basedir}" includes="${app-name}.jar"/>
        </copy>
        <move file="${codemodule.dir}/${app-name}.jar" tofile="${codemodule.dir}/0_${app-name}.jar"/>
        <zip destfile="${codemodule.dir}/${app-name}-fat.jar" duplicate="preserve">
          <zipgroupfileset dir="${codemodule.dir}">
            <include name="*.jar"/>
          </zipgroupfileset>
        </zip>
    

    0 讨论(0)
  • 2020-11-28 06:03

    Sure, just extract the two jar files and recreate a new one

    $ mkdir tmp
    $ (cd tmp; unzip -uo ../jar1.jar)
    $ (cd tmp; unzip -uo ../jar2.jar)
    $ jar -cvf combined.jar -C tmp .
    

    The stuff with tmp ensures that the two existing jars are extracted into a clean directory and then the new one made from that.

    Be aware that you may also need to merge any manifest.mf files contained therein, and if there are any also include the '-m' option in that file command.

    0 讨论(0)
  • 2020-11-28 06:04
    <?xml version="1.0" encoding="UTF-8"?>
    <project name="zip-test" default="zip" basedir=".">
    
        <target name="zip">
            <zip destfile="out.jar">
                <zipgroupfileset dir="." includes="*.jar"/>
            </zip>
        </target>
    </project>
    

    save this code in build.xml file and keep it in same folder where all the jar files to be combined are kept. Open cmd, give path of folder and run command : ant zip.

    It will generate out.jar which is combination of all jars.

    0 讨论(0)
  • 2020-11-28 06:10

    Extract both jars and create a new one works. (Use jar commands shown above). One caveat about manifest file is that you want to extract the jar whose manifest file you want to retain in the last.

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

    Use zipgroupfileset with the Ant Zip task

    <zip destfile="out.jar">
        <zipgroupfileset dir="lib" includes="*.jar"/>
    </zip>
    

    Might help you.

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