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


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

搜索关键字:

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,大部分的源码都还是可以免费得到的,在反编译之前,先搜搜看能否找到源码吧,免得白忙活~~

,

《 “将.class文件反编译为.java文件” 》 有 8 条评论

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

  2. 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];
    }
    `

  3. 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
    `

回复 hi 取消回复

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