Comparing two .jar files

JavaJarComparisonDiff

Java Problem Overview


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

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

Java Solutions


Solution 1 - Java

Solution 2 - Java

If you select two files in IntellijIdea and press Ctrl + Dthen it will show you the diff. I use Ultimate and don't know if it will work with Community edition.

Solution 3 - Java

  1. Rename .jar to .zip
  2. Extract
  3. Decompile class files with jad
  4. Recursive diff

Solution 4 - Java

Extract each jar to it's own directory using the jar command with parameters xvf. i.e. jar xvf myjar.jar for each 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).

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.

Solution 5 - Java

Create a folder and create another 2 folders inside it like old and new. add relevant jar files to the folders. then open the first folder using IntelliJ. after that click whatever 2 files do you want to compare and right-click and click compare archives.

Solution 6 - Java

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

Solution 7 - Java

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

	#!/bin/sh
	
	# Needed if running on Windows
	FIND="/usr/bin/find"
	DIFF="diff -r"
	
	# Extract the jar (war or ear)
	JAR_FILE1=$1
	JAR_FILE2=$2
	
	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.

Solution 8 - Java

Use Java Decompiler to turn the jar file into source code file, and then use WinMerge to perform comparison.

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

Solution 9 - Java

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

#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

Solution 10 - Java

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

then use meld diff viewer and compare as folders ..

Solution 11 - Java

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

Solution 12 - Java

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

Solution 13 - Java

This application may be what you need, works great and display a simple GUI showing differences. Try Jarcomp

Solution 14 - Java

If you are using IntelliJ IDEA or Android Studio, add your jar files to a project under the libs folder. Then select the both jar files, right click then select "Compare Archives"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKunalView Question on Stackoverflow
Solution 1 - JavalinuxbuildView Answer on Stackoverflow
Solution 2 - JavaxueshengView Answer on Stackoverflow
Solution 3 - Javasje397View Answer on Stackoverflow
Solution 4 - JavaBrianView Answer on Stackoverflow
Solution 5 - JavaRajitha BhanukaView Answer on Stackoverflow
Solution 6 - JavaFoxyBOAView Answer on Stackoverflow
Solution 7 - JavaskangaView Answer on Stackoverflow
Solution 8 - JavaCheok Yan ChengView Answer on Stackoverflow
Solution 9 - JavaSidJView Answer on Stackoverflow
Solution 10 - JavaDulip ChandanaView Answer on Stackoverflow
Solution 11 - JavaTomView Answer on Stackoverflow
Solution 12 - JavaJayanView Answer on Stackoverflow
Solution 13 - JavaZvonkoView Answer on Stackoverflow
Solution 14 - JavaMahmoudView Answer on Stackoverflow