0

In my Java application, I wish to read bytecode contents from class files that aren't actually loaded, in jar files which also aren't loaded. As in, I need to be able to take any given jarfile, and find all classes inside it, ideally. So take the following situation:

My application (which is kind of a library), is asked to 'check' a certain jar at whatever path, and is provided various patterns provided by the application using my library to find similarities (such as constant pool similarities). Therefore my library needs to go through all the jars in a class file. Obviously I could make it hardcoded or loaded from a file, but I'd much rather have it so that I can go through the bytecode of all the files in a jar to match them.

OllieStanley
  • 712
  • 7
  • 25
  • What are you trying to accomplish by "checking" the jars? Do you have to deal with potentially malicious classfiles? – Antimony Apr 13 '13 at 19:27
  • Checking for matches with specific patterns in the bytecode, to identify things from one jar that are the same as from another jar, but renamed / obfuscated. – OllieStanley Apr 13 '13 at 19:37
  • Checking exact sequences of bytes won't help against more advanced obfuscators. – Antimony Apr 13 '13 at 19:58
  • It doesn't need to be able to work against advanced obfuscators. Sometimes it won't even be used on obfuscated jars. It's part of an auto-updater. – OllieStanley Apr 13 '13 at 20:07
  • Can't you just open the jars and search the files for different bytestrings then? – Antimony Apr 13 '13 at 20:47
  • Well I need to get all the .class files inside the .jar from my application, not manually. I just don't know how to go about finding all the class resources in a .jar file – OllieStanley Apr 13 '13 at 20:52

1 Answers1

2

You should use the JarFile API and iterate over the files in it. It shouldn't be hard to do. This article might be a good start.

And as for the bytecode you could just treat each (uncompressed) class file as a byte array and calculate a hash, maybe an MD5 hash of each file and compare it to previous hash.

Cebence
  • 2,406
  • 2
  • 19
  • 20