将.class文件反编译为.java文件

本文最后更新于2015年1月21日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

工作中碰到了这样的需求,记得之前试过一些,这次又找到了不错的说明,遂记录一下,做个备忘:

搜索关键字:

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:

  1. Procyon: open-source (Apache 2) and actively developed
  2. Krakatau: open-source (GPLv3) and actively developed
  3. CFR: open-source (MIT) and actively developed
  4. JAD
  5. DJ Java Decompiler
  6. Mocha

And many more.

These produce Java code. Java comes with something that lets you see JVM byte code (javap).

 

补充说明:

上面的内容挺初级的,对于一些做过特殊处理的.class文件就没什么办法了,这里就当是多了解一些知识吧。现在有了GitHub,大部分的源码都还是可以免费得到的,在反编译之前,先搜搜看能否找到源码吧,免得白忙活~~

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/1811.html

6 thoughts on “将.class文件反编译为.java文件”

  1. 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. 总结
    `

发表评论

电子邮件地址不会被公开。 必填项已用*标注