Search code examples
javadecompiler

How do I decompile a java class file?


I understand that similar questions to this have been asked, but non of them have answered how to decompile a java class with java. I want to make a java decompiler in java but I do not know how to take the contents of a class file and convert it to a java file.

I am quite sure I will be able to open up a jar file and view it's contents, but my question is how do I decompile the class files in the jar? I do not want a decompiler; I want to make my own.

I am assuming that every compiler for java encrypts the code the same way and I just want to know what that is or how to reverse it. I do not know much about how java code is compiled, but I am pretty sure it has something to do with the jdk developer kit.

Lets say I have an entire class loaded into a byte array. How would I convert the byte array to readable code.

I have a java decompiler right now, but it is an executable, so I can not decompile it and see how it is done.

I understand that it will take a lot of information to answer this question, and I have very little knowledge of how Java compiles java file, so if you could point me in the right direction to learn how java compiles stuff. Could you simply post links to tutorials or things I should read to learn how to decompile a class or how java compiles classes in the first place?


Solution

  • I'd start by investigating the class file format. Building something to parse its structure and jump in from there.

    When you eventually write the code for handling code attributes in the class file, all of the bytecode for a given method will be in an array. From there you'd start applying the JVM specification for how to decompile specific instructions.

    There are also libraries that provide much of this functionality already and can help you bootstrap yourself.