Analyzing the code of the class files in a jar file

Viewed 83

I'm trying to analyze the contents of android apps. I already made the code I need to analyze it if it was a human readable file format, like .java or .txt. I know how to change the decompile the apk to get the dex file and how to change the dex file into a jar file but, now, I need to be able to analyze a jar file to analyze its .class files to convert them to .java files. I was trying to use jd-core because jd-gui works so well for decompiling jar files, but it doesn't seem to be working for me.

I have this code snippet that unzips the jar file from this StackOverflow answer:

java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);
java.util.Enumeration enumEntries = jar.entries();
while (enumEntries.hasMoreElements()) {
  java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();
  java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());
  if (file.isDirectory()) { // if its a directory, create it
    f.mkdir();
    continue;
  }
  java.io.InputStream is = jar.getInputStream(file); // get the input stream
  java.io.FileOutputStream fos = new java.io.FileOutputStream(f);
  while (is.available() > 0) {  // write contents of 'is' to 'fos'
    fos.write(is.read());
  }
  fos.close();
  is.close();
}

But I can't find any way to turn the class files into .java files. Also, this unzips very very slowly. I feel like I may be going about this completely wrong. Any help you can give me?

1 Answers

Class files are compiled files, which means the only way to read the code inside them would be to decompile them. class files contain byte code that is not human-readable and is not meant to be. What you could do is use a decompiler programmatically.

Check this and this.

Related