简体   繁体   中英

Byte code to Java source code

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?

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.

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.

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:

  • Procyon
  • CFR
  • JD
  • Fernflower

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/

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM