Reputation: 21921
Is it possible to convert a .class
file to .java
file?
How can this be done?
What about the correctness of the code extracted from this option?
Upvotes: 27
Views: 31681
Reputation: 45
I have used intellij idea community edition, where i noticed that I could open the.class files too and that the decompiled code was exactly the same as I had encoded it.
Though I am not sure if it was due to the compilation to the .class file in the same ide as i opened it.
Also the accuracy of the result may be different for other's use case.
Upvotes: 0
Reputation: 943
Adding to the previous answers: recently, a new wave of decompilers has been coming, namely Procyon, CFR, JD, Fernflower
Here's a list of modern decompilers as of March, 2015:
You may test above mention decompilers online, no installation required and make your own educated choice. Java decompilers in the cloud: http://www.javadecompilers.com/
Upvotes: 2
Reputation: 193804
It is possible. You need a Java Decompiler to do this.
You'll find mostly it'll do a surprisingly good job. What you'll get is a valid .java
file which will compile to the .class
file but this .java
file won't necessarily be the same as the original source code. Things like looping constructs might come out differently, and anything that's compile time only such as generics and annotations won't be re-created.
You might have a problem if the code has been obfuscated. This is a process which alters the class files to make them hard to decompile. For example, class and variable names are changed to all be similar so you'll end up with code like aa.a(ab)
instead of employee.setName(name)
and it's very hard to work out what's going on.
I remember using JAD to do this but I don't think this is actively maintained so it may not work with never versions of Java. A Google search for java decompiler will give you plenty of options.
Upvotes: 28
Reputation: 36799
DJ is the easy to use java decompiler . Just open any .class file and it will show you its java
code.
Also, you can use jadClipse plugin for eclipse to directly decompile the .class into .java
What about the correctness of the code extracted from this option?
In any case, the code which will be generated by any java decompiler will not be the same as it was written in orginal java class. As it just decodes the bytecode into java code. The only thing you can be sure is, that the output will be same as the output of orginal java code.
Upvotes: 0
Reputation: 36115
Not exactly a decompiler, but the JDK contains javap, a disassembler:
javap -c org.example.MyClass
Depending on your usecase, it might still be interesting to know or use.
Note that results of class file decompilation depend on the included information within a class file. If I remember correctly, included debug information (see -g flag of javac) is important, especially for naming of variables and the like.
Upvotes: 0
Reputation: 26138
This is possible using one of the available Java decompilers. Since you are working from byte-code which may have been optimised by the compiler (inlining static variables, restructing control flow etc) what you get out may not be exactly the same as the code that was originally compiled but it will be functionally equivalent.
Upvotes: 2
Reputation: 5523
It is always possible. Search for "java disassembler".
But source code comments and temporary variables will not be available.
If you decompile a class and see the code is too complex with variable names and method names are like a,b,c... that means that the project is obfuscated.
Upvotes: 0