Meaningless Drivel is fun!
The moose likes EJB and other Java EE Technologies and the fly likes How to Compare contents of Jar files? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to Compare contents of Jar files?" Watch "How to Compare contents of Jar files?" New topic

How to Compare contents of Jar files?

Krishna Mohan
Ranch Hand

Joined: May 30, 2003
Posts: 44
I have a requirement to compare two jar files(same size) and determine whether they are built out of exact same source files.
Is there a way to determine this ?Are there any tools available?
Any suggestions ?
(PS :At the moment I do not have access to the source to build a new jar )

Programming is Fun.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Can you trust that the .class files were compiled using the same compiler?
Can you "fail" the comparison test because the other .jar file has a different or some other non-code resource?
The simplest option would be to calculate a hash (MD5 for example) from the .jar file's raw bytes and compare those. You could also, read the .jar files' contents using java.util.jar.* and do the same hash comparison for each file individually (filtering out any irrelevant files).
If the hashing stuff doesn't seem to fit, you could try exploding the jar files somewhere, run a decompiler on them, and then compare the resulting source files.
Yet another way could be to compare the timestamps within the .jar files (assuming that the original timestamps were preserved when the .jar file was created).

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Todd Breiholz

Joined: Dec 19, 2003
Posts: 2
Here's some code I wrote a short time ago to compare 2 jars by comparing the CRC checksum of each JarEntry within.

Hope this helps
Todd Breiholz
I agree. Here's the link:
subject: How to Compare contents of Jar files?
It's not a secret anymore!