工作中碰到了这样的需求,记得之前试过一些,这次又找到了不错的说明,遂记录一下,做个备忘:
搜索关键字:
http://search.aol.com/aol/search?q=convert+class+to+.java
一、使用javap命令(Java自带)
http://stackoverflow.com/questions/6225537/convert-class-to-java
The javap command takes class-names without the .class extension. Try
C:\> javap -c ClassName
javap will however not give you the implementations of the methods in java-syntax. It will at most give it to you in JVM bytecode format.
To actually decompile (i.e., do the reverse of javac) you will have to use proper decompiler. See for instance the following related question:
How do I “decompile” Java class files?
二、使用工具
http://stackoverflow.com/questions/272535/how-do-i-decompile-java-class-files
- The final release of JSR 176, defining the major features of J2SE 5.0 (Java SE 5), has been published on September 30, 2004.
- The lastest Java version supported by JAD, the famous Java decompiler written by Mr. Pavel Kouznetsov, is JDK 1.3.
- Most of the Java decompilers downloadable today from the Internet, such as “DJ Java Decompiler” or “Cavaj Java Decompiler”, are powered by JAD: they can not display Java 5 sources.
Java Decompiler (Yet another Fast Java decompiler) has:
- Explicit support for decompiling and analyzing Java 5+ “.class” files.
- A nice GUI.
It works with compilers from JDK 1.1.8 up to JDK 1.7.0, and others (Jikes, JRockit, etc.).
It features an online live demo version that is actually fully functional! You can just drop a jar file on the page and see the decompiled source code without installing anything.
====
There are a few decompilers out there… A quick search yields:
- Procyon: open-source (Apache 2) and actively developed
- Krakatau: open-source (GPLv3) and actively developed
- CFR: open-source (MIT) and actively developed
- JAD
- DJ Java Decompiler
- Mocha
And many more.
These produce Java code. Java comes with something that lets you see JVM byte code (javap).
补充说明:
上面的内容挺初级的,对于一些做过特殊处理的.class文件就没什么办法了,这里就当是多了解一些知识吧。现在有了GitHub,大部分的源码都还是可以免费得到的,在反编译之前,先搜搜看能否找到源码吧,免得白忙活~~
《 “将.class文件反编译为.java文件” 》 有 8 条评论
jvm系列(一):java类的加载机制
https://mp.weixin.qq.com/s/rLooaTOU_NQTJdn28KAUFw
批量反编译jar包
http://www.03sec.com/3213.shtml
java反编译
http://www.03sec.com/3189.shtml
JVM 揭秘:一个 class 文件的前世今生
https://mp.weixin.qq.com/s/PogUTW9hhAOibZCgpLPe_w
`
1. 准备
2. class 文件的结构
3. class文件的重要组成
3.1 常量池
3.2 字段
3.3 方法
3.4 属性
4. 执行引擎
4.1 运行时栈帧结构
4.2 JVM的指令
4.3 几个常用的指令解析
5. 总结
`
awesome-decompilation 整理 ,来自 nforest_ ‘s weibo
https://m.weibo.cn/status/4229107040613121
https://github.com/nforest/awesome-decompilation
https://drive.google.com/drive/folders/0B1X32SwXTZPuYWwxWW5BNi1oWDA
终端反编译字节码利器 CFR
https://droidyue.com/blog/2019/02/24/decompile-class-file-command-line/
http://www.benf.org/other/cfr/index.html
IDEA反编译出整个jar包源码
https://blog.csdn.net/u013279563/article/details/89556869
https://github.com/fesh0r/fernflower
IDEA反编译jar包源码
https://www.cnblogs.com/Jeely/p/11400604.html
Idea打包jar 及jar包反编译为代码的多种方法
https://blog.csdn.net/qq_37939251/article/details/83628878
Chapter 4. The class File Format
https://docs.oracle.com/javase/specs/jvms/se15/html/jvms-4.html
`
Java源文件(*.java)通过编译后会变成class文件,class文件有固定的二进制格式
A class file consists of a single ClassFile structure:
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
`
CFR – another java decompiler
http://www.benf.org/other/cfr/index.html
https://github.com/leibnitz27/cfr/releases
`
CFR will decompile modern Java features – up to and including much of Java 9, 12 & 14, but is written entirely in Java 6, so will work anywhere! (FAQ) – It’ll even make a decent go of turning class files from other JVM languages back into java!
To use, simply run the specific version jar, with the class name(s) you want to decompile (either as a path to a class file, or as a fully qualified classname on your classpath). (–help to list arguments).
Alternately, to decompile an entire jar, simply provide the jar path, and if you want to emit files (which you probably do!) add –outputdir /tmp/putithere
$ java -jar cfr-0.152.jar –help
$ java -jar cfr-0.152.jar ./test.class
$ java -jar cfr-0.152.jar ./test.class > ./test-decompile.java
`