English 中文(简体)
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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签