-1

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?

Forseth11
  • 1,418
  • 1
  • 12
  • 21
  • 1
    Just flip the bits and twiddle and bam, you've got Java source code. What are you expecting an answer to say? Decompiling a class file to source code is extremely complicated. – Radiodef May 11 '15 at 02:48
  • 4
    You need to read [The Java Virtual Machine Specification](https://docs.oracle.com/javase/specs/jvms/se7/html/index.html) very carefully. Unfortunately, this question is far too broad so I'm voting to close it. – Greg Hewgill May 11 '15 at 02:48
  • 1
    You can fairly easily write a clone of *javap*, the class file disassembler. (I did it once in the space of a few days.) As suggested, study the JVM spec document, then just read the class file as a file of bytes and decode the bytes per the JVM spec. Creating a true "decompiler", that will return something resembling the Java source code, is far more challenging -- you'd best study compiler writing techniques first. – Hot Licks May 11 '15 at 03:00
  • 1
    Compilation is not encryption. – user207421 May 11 '15 at 03:07
  • By the way it's wrong that every compiler generates the same bytecode. I know an example when ecj and javac generate even different number of .class-files when compiling the same source, not to mention different synthetic methods with different signatures, etc. So you should be able to deal with compiler-specific behavior as well. – Tagir Valeev May 31 '15 at 19:10

1 Answers1

2

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.

Alfred Rossi
  • 1,942
  • 15
  • 19