Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Compare contents of Jar files?

 
Krishna Mohan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
TIA
Krishna
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 manifest.mf 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).
 
Todd Breiholz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic