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
Comparing two .jar files
提问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
回答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.jar
for each jar.
使用带有参数 xvf 的 jar 命令将每个 jar 解压缩到它自己的目录。即jar xvf myjar.jar
每个罐子。
Then, use the UNIX command diff
to compare the two directories. This will show the differences in the directories. You can use diff -r dir1 dir2
two 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 linuxbuild
JAPICC, sample usage:
japi-compliance-checker OLD.jar NEW.jar
Sample reports for log4j: http://abi-laboratory.pro/java/tracker/timeline/log4j/
PkgDiff, sample usage:
pkgdiff OLD.jar NEW.jar
See sample reportfor args4j.
Clirr, sample usage:
java -jar clirr-core-0.6-uber.jar -o OLD.jar -n NEW.jar
回答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,然后从那里执行脚本。