java 字节码转Java源代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1832000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:09:47  来源:igfitidea点击:

Byte code to Java source code

javabytecode

提问by Sachin Chourasiya

Is it possible to convert a .classfile to .javafile?

是否可以将.class文件转换为.java文件?

How can this be done?

如何才能做到这一点?

What about the correctness of the code extracted from this option?

从这个选项中提取的代码的正确性如何?

采纳答案by Dave Webb

It is possible. You need a Java Decompiler to do this.

有可能的。您需要一个 Java 反编译器来执行此操作。

You'll find mostly it'll do a surprisingly good job. What you'll get is a valid .javafile which will compile to the .classfile but this .javafile 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.

你会发现大多数情况下它会做得非常好。您将获得一个有效.java文件,该.class文件将编译为该文件,但该.java文件不一定与原始源代码相同。循环结构之类的东西可能会以不同的方式出现,并且不会重新创建任何仅在编译时使用的东西,例如泛型和注释。

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.

如果代码被混淆,您可能会遇到问题。这是一个更改类文件以使其难以反编译的过程。例如,类名和变量名都更改为相似,因此您最终会得到类似aa.a(ab)而不是的代码,employee.setName(name)并且很难弄清楚发生了什么。

I remember using JADto 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 decompilerwill give you plenty of options.

我记得使用JAD来做到这一点,但我不认为这是积极维护的,所以它可能不适用于从未版本的 Java。一个谷歌搜索的Java反编译器会给你很多的选择。

回答by Tendayi Mawushe

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.

这可以使用可用的Java 反编译器之一来实现。由于您正在使用可能已被编译器优化的字节码(内联静态变量、重构控制流等),您得到的内容可能与最初编译的代码不完全相同,但在功能上是等效的。

回答by Andrew Rukin

Adding to the previous answers: recently, a new wave of decompilers has been coming, namely Procyon, CFR, JD, Fernflower

补充一下之前的回答:最近又来了一波反编译器,分别是Procyon、CFR、JD、Fernflower

Here's a list of modern decompilers as of March, 2015:

以下是截至 2015 年 3 月的现代反编译器列表:

  • 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/

您可以在线测试上述反编译器,无需安装并做出您自己的受过教育的选择。 云端 Java 反编译器:http : //www.javadecompilers.com/

回答by sfussenegger

Not exactly a decompiler, but the JDK contains javap, a disassembler:

不完全是反编译器,但JDK 包含 javap,一个反汇编器

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.

请注意,类文件反编译的结果取决于类文件中包含的信息。如果我没记错的话,包含的调试信息(参见 javac 的 -g 标志)很重要,尤其是对于变量的命名等。

回答by Rakesh Juyal

DJis the easy to use java decompiler. Just open any .class file and it will show you its javacode.

DJ是易于使用的 java反编译器。只需打开任何 .class 文件,它就会显示它的java代码。

Also, you can use jadClipseplugin for eclipse to directly decompile the .class into .java

另外,你可以使用eclipse的jadClipse插件直接将.class反编译成.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.

What about the correctness of the code extracted from this option?
在任何情况下,任何 Java 反编译器生成的代码都不会与在原始 Java 类中编写的代码相同。因为它只是将字节码解码为 java 代码。您唯一可以确定的是,输出将与原始 Java 代码的输出相同。

回答by JCasso

It is always possible. Search for "java disassembler".

这总是可能的。搜索“java 反汇编程序”。

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.

如果你反编译一个类,看到代码太复杂,变量名和方法名像a,b,c……那说明项目被混淆了。