tags:

views:

118

answers:

8
+2  Q: 

Byte code to java

Is it possible to convert a .class file to .java file. If yes then how? What about the correctness of the code extracted from this option?

+1  A: 

Yes, try JAD Java Decompiler

oshyshko
+7  A: 

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.

Dave Webb
A: 

It is possible, but the code you will get can be incorrect. Search for some Java decompilers though.

Yurish
A: 

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.

JCasso
I think it is not garanted that it is ALWAYS possible. I think there a are some bytecode constructs that have no equivalent in Java, also some languages/tools that genrate .class from other sources (Scala...).
Carlos Heuberger
Funny... We are talking about Java and bytecode here.
JCasso
so am I... but OP has not mentioned that the .class was created (directly) from Java. And thanks...
Carlos Heuberger
Have you seen the tags :)
JCasso
sure, but the `java` tag can be just for the target file (.java). It does not say that the .class was generated from Java... Have you read the question :--) surely it's my fault since my english is that bad (neither first nor second language) but I'm sure this is not the correct media for this kind of discussion, and not leading anywhere... I'm out!
Carlos Heuberger
+1  A: 

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.

Tendayi Mawushe
A: 

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.

sfussenegger
A: 

I have been using JAD, but later switched to another one, which in my opinion is better - here

Bozho
A: 

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.

Rakesh Juyal
+1 for the output will be same as the output of orginal java code
Sachin Chourasiya