简体   繁体   English

如何组合两个Jar文件

[英]How to combine two Jar files

Is it possible to combine two jar files such that in an applet tag I can simply do something like 是否可以组合两个jar文件,以便在applet标签中我可以简单地做类似的事情

archive="jarjar.jar/jar1.jar"...  ...archive="jarjar.jar/jar2.jar"... instead of
archive="jar1.jar"... ...archive="jar2.jar"...

I need to only have one jar file so putting two jar files in a folder will not help me. 我只需要一个jar文件,所以将两个jar文件放在一个文件夹中对我没用。

Sure, just extract the two jar files and recreate a new one 当然,只需提取两个jar文件并重新创建一个新文件

$ 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. 使用tmp的东西确保将两个现有的jar提取到一个干净的目录中,然后从中创建新的jar。

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. 请注意,您可能还需要合并其中包含的任何manifest.mf文件,如果还有任何在该文件命令中也包含'-m'选项。

You need to extract both the JAR files and regenerate one. 您需要提取两个JAR文件并重新生成一个。

 
 
 
 
  
  
  jar -xvf jar1.jar tmp jar -xvf jar2.jar tmp cd tmp jar -cvf jar3.jar .
 
 
  

Please refer to the below answer . 请参考以下答案

Use zipgroupfileset with the Ant Zip task 将zipgroupfileset与Ant Zip任务一起使用

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

Might help you. 可能会帮助你。

If you are using gradle, just add the following to build.gradle. 如果您使用的是gradle,只需将以下内容添加到build.gradle即可。 No plugins required. 无需插件。 If you need special options, then go with Fatjar plugin, as initialZero suggests. 如果您需要特殊选项,那么请使用Fatjar插件,正如initialZero建议的那样。

task superSimpleJar(type: Jar) {
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it :     zipTree(it) } }
    with jar
}

For Android project, add this to app/build.gradle and run "gradlew superSimpleJar". 对于Android项目,将其添加到app / build.gradle并运行“gradlew superSimpleJar”。 Find jar in build/libs/app-all.jar 在build / libs / app-all.jar中找到jar

task superSimpleJar(type: Jar) {
baseName = project.name + '-all'
from {

    configurations.compile.findAll {

    it.getName() != 'android.jar'

    }.collect {

    it.isDirectory() ? it : zipTree(it)

    }
 }
}
<?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. 将此代码保存在build.xml文件中,并将其保存在同一个文件夹中,以保存要组合的所有jar文件。 Open cmd , give path of folder and run command : ant zip . 打开cmd ,给出文件夹路径并运行命令: ant zip

It will generate out.jar which is combination of all jars. 它将生成out.jar ,它是所有罐子的组合。

Just unzip both jar files, then zip the results into one zip file, and rename this to jar again. 只需解压缩两个jar文件,然后将结果压缩成一个zip文件,然后再将其重命名为jar。

But as adarshr said: Better use the jar command for that. 但正如adarshr所说:更好地使用jar命令。

还有eclipse fatjar插件。

Extract both jars and create a new one works. 提取两个罐子并创建一个新的工作。 (Use jar commands shown above). (使用上面显示的jar命令)。 One caveat about manifest file is that you want to extract the jar whose manifest file you want to retain in the last. 关于清单文件的一个警告是,您要提取要在最后保留其清单文件的jar。

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. 我确实看到sumanth.donthula的答案中的值,因为我们所有人合并jar的问题将是如何处理清单文件。 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. 在我的情况下,我想将一些库文件(主要是生成的Web服务客户端代码)合并到我编写的应用程序的jar中。 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). 最简单的方法是处理解压缩原始文件的顺序(如Alnitak和sumanth.donthula所述)。

I wanted to use the zip ant task (thank you, ykombinator, for the idea). 我想使用zip ant任务(谢谢你,ykombinator,这个想法)。 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). 我的示例中的输出目录称为codemodule.dir(我创建了一个FileNet代码模块)。 The rest of the names are self-explaining. 其余的名字都是自我解释的。 The important step is renaming the application jar to 0_... to be the 1st in order. 重要的一步是将应用程序jar重命名为0 _...按顺序排列第一。 This way its manifest will be retained as the duplicate attribute of the zip ant task is set to preserve. 这样,当zip ant任务的重复属性设置为保留时,其清单将被保留。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM