Java 比较两个 .jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3868803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:05:46  来源:igfitidea点击:

Comparing two .jar files

javajarcomparisondiff

提问by Kunal

How do I compare two .jar files? Both of them have compiled .class files.

如何比较两个 .jar 文件?他们都编译了 .class 文件。

I want the difference in terms of method changes, etc.

我想要在方法更改等方面的差异。

回答by Cheok Yan Cheng

Use Java Decompilerto turn the jar file into source code file, and then use WinMergeto perform comparison.

使用Java Decompiler将jar文件转成源代码文件,然后使用WinMerge进行比较。

You should consult the copyright holder of the source code, to see whether it is OK to do so.

您应该咨询源代码的版权所有者,看看是否可以这样做。

回答by sje397

  1. Rename .jar to .zip
  2. Extract
  3. Decompile class files with jad
  4. Recursive diff
  1. 将 .jar 重命名为 .zip
  2. 提炼
  3. jad反编译class文件
  4. 递归差异

回答by Tom

Here's an aparently free tool http://www.extradata.com/products/jarc/

这是一个明显免费的工具http://www.extradata.com/products/jarc/

回答by Brian

Extract each jar to it's own directory using the jar command with parameters xvf. i.e. jar xvf myjar.jarfor each jar.

使用带有参数 xvf 的 jar 命令将每个 jar 解压缩到它自己的目录。即jar xvf myjar.jar每个罐子。

Then, use the UNIX command diffto compare the two directories. This will show the differences in the directories. You can use diff -r dir1 dir2two recurse and show the differences in text files in each directory(.xml, .properties, etc).

然后,使用 UNIX 命令diff比较这两个目录。这将显示目录中的差异。您可以使用diff -r dir1 dir2两个递归并显示每个目录(.xml、.properties 等)中文本文件的差异。

This will also show if binary class files differ. To actually compare the class files you will have to decompile them as noted by others.

这还将显示二进制类文件是否不同。要实际比较类文件,您必须按照其他人的说明对它们进行反编译。

回答by Jayan

Please try http://www.osjava.org/jardiff/- tool is old and the dependency list is large. From the docs, it looks like worth trying.

请尝试http://www.osjava.org/jardiff/- 工具很旧并且依赖项列表很大。从文档来看,它看起来值得一试。

回答by FoxyBOA

I use to ZipDifflib (have both Java and ant API).

习惯使用ZipDiff库(有 Java 和 ant API)。

回答by linuxbuild

回答by SidJ

In Linux/CygWin a handy script I use at times is:

在 Linux/CygWin 中,我有时使用的一个方便的脚本是:

#Extract the jar (war or ear)
cd dir1
jar xvf jar-file1
for i in `ls *.class`
do
 javap $i > ${i}.txt #list the functions/variables etc
done

cd dir2
jar xvf jar-file2
for i in `ls *.class`
do
 javap $i > ${i}.txt #list the functions/variables etc
done

diff -r dir1 dir2 #diff recursively

回答by Dulip Chandana

use java decompilerand decompile all the .class files and save all files as project structure .

使用 java反编译器反编译所有.class文件并将所有文件保存为项目结构。

then use meld diff viewer and compare as folders ..

然后使用meld diff查看器并作为文件夹进行比较..

回答by skanga

Here is my script to do the process described by sje397:

这是我执行 sje397 描述的过程的脚本:

    #!/bin/sh

    # Needed if running on Windows
    FIND="/usr/bin/find"
    DIFF="diff -r"

    # Extract the jar (war or ear)
    JAR_FILE1=
    JAR_FILE2=

    JAR_DIR=${PWD}          # to assign to a variable
    TEMP_DIR=$(mktemp -d)

    echo "Extracting jars in $TEMP_DIR"

    EXT_DIR1="${TEMP_DIR}/${JAR_FILE1%.*}"
    EXT_DIR2="${TEMP_DIR}/${JAR_FILE2%.*}"

    mkdir ${EXT_DIR1}
    cd ${EXT_DIR1}
    jar xf ${JAR_DIR}/${JAR_FILE1}
    jad -d . -o -t2 -safe -space -b -ff -s java -r **/*.class
    cd ..

    mkdir ${EXT_DIR2}
    cd ${EXT_DIR2}
    jar xf ${JAR_DIR}/${JAR_FILE2}
    jad -d . -o -t2 -safe -space -b -ff -s java -r **/*.class
    cd ..

    # remove class files so the diff is clean
    ${FIND} ${TEMP_DIR} -name '*.class' | xargs rm

    # diff recursively 
    ${DIFF} ${EXT_DIR1} ${EXT_DIR2}

I can run it on Windows using GIT for Windows. Just open a command prompt. Run bash and then execute the script from there.

我可以使用 GIT for Windows 在 Windows 上运行它。只需打开一个命令提示符。运行 bash,然后从那里执行脚本。