Java反编译工具Jad详解

做项目过程中需要反编译一个jar包,于是作了一些学习,记录下来。

Jad(JAvaDecompiler)是一个Java的反编译器,可以通过命令行把Java的class文件反编译成源代码。

如果你在使用编写代码的时候遇到找不到源代码的第三方库,而且你又需要看看这个库的具体实现,那么你就需要使用这个东西。而且如果你是使用Eclipse的话,还有一个Eclipse插件JadClipseJadClipse提供一个class文件查看器可以直接打开class文件查看其反编译后的源代码, 太方便了,太方便了!

不过,可惜的是这个开源项目似乎已经关闭了,官方网站已经上不去了,有个叫Tomas Varaneckas的好心人收集了所有的版本提供给大家下载:JAD Java Decompiler Download

而且这个最新的版本也只支持Java 4,不支持Java 5。还有一个叫做JD-Core的工具支持Java 5的新特性,如enum等,而且它提供独立的GUI程序JD-GUI和基于Eclipse的插件JD-Eclipse,也很方便。但是对于JD-Core,作者没有提供下载的链接,他说“Open the sources of JD-Core is not on my roadmap: I spent too many time on this project.”当然后面也有一大篇文章是别人用来骂他的,可以看Add JD-Core to the download list,蛮有意思。

我的项目中需要将第三方库反编译出来,让后对其进行修改,然后再编译,不仅仅是查看,因此只能用命令行的Jad来做。

使用方法:

[1]反编译一个class文件:jad example.class,会生成example.jad,用文本编辑器打开就是java源代码

[2]指定生成源代码的后缀名:jad -sjava example.class,生成example.java

[3]改变生成的源代码的名称,可以先使用-p将反编译后的源代码输出到控制台窗口,然后使用重定向,输出到文件:jad -p example.class > myexample.java

[4]把源代码文件输出到指定的目录:jad -dnewdir -sjava example.class,在newdir目录下生成example.java

[5]把packages目录下的class文件全部反编译:jad -sjava packages/*.class

[6] 把packages目录以及子目录下的文件全部反编译:jad -sjava packages/**/*.class,不过你仍然会发现所有的源代码文件被放到了同一个文件中,没有按照class文件的包路径建立起路径

[7] 把packages目录以及子目录下的文件全部反编译并建立和java包一致的文件夹路径,可以使用-r命令:jad -r -sjava packages/**/*.class

[8] 当重复使用命令反编译时,Jad会提示“whether you want to overwrite it or not”,使用-o可以强制覆盖旧文件

[9] 还有其他的参数可以设置生成的源代码的格式,可以输入jad命令查看帮助,详见下文。

[10] 当然,你会发现有些源文件头部有些注释信息,不用找了,jad没有参数可以去掉它,用别的办法吧。

最后,jad不能直接反编译jar包,直接解压了再反编译吧(这个方法很显然,我实在不好意思写出来)。



jad是最简单的class反编译为java文件的小工具.

现在就总结一下jad 命令

首先,看一下jad的帮助提示

-a - 用JVM字节格式来注解输出
-af - 同 -a,但是注解的时候用全名称
-clear - 清除所有的前缀
-b - 输出多于的括号 (e.g., if(a) { b(); }, default: no)
-d <dir> - 指定输出文件的文件目录
-dead -试图反编译代码的dead 部分(default: no)
-disass - 不用用字节码的方式反编译 (no JAVA source generated)
-f - 输出整个的名字,无论是类还是方法
-ff -输出类的成员在方法之前 (default: after methods)
-i - 输出所有的变量的缺省的最初值
-l<num> - 将strings分割成指定数目的块的字符 (default: no)
-lnc - 将输出文件用行号来注解 (default: no)
-nl - 分割strings用新行字符 newline character (default: no)
-nodos -不要去检查class文件是否以dos方式写 (CR before NL, default: check)
-nocast - 不要生成辅助文件
-nocode -不要生成方法的源代码
-noconv - 不要转换java的定义符 (default: do)
-noctor - 不允许空的构造器存在
-noinner -关掉对内部类的支持 (default: turn on)
-nolvt - 忽略局部变量的表信息
-nonlb - 不要输出一个新行在打开一个括号之前 (default: do)
-o - 无需确认直接覆盖输出 (default: no)
-p - 发送反编译代码到标准输出 STDOUT (e.g., for piping)

其次.常用命令

jad -o -r -sjava -dsrc test.class

tree目录下的所有*.class文件
jad -o -r -sjava -dsrc tree/**/*.class

unix可以表示为:jad-o-r-sjava-dsrc'tree/**/*.class'

指定输出文件的名字的话,用以下的转移命令

jad-pexample1.class>myexm1.java

输出带字节码注释和源码的(最常用)

jad -o -a Test.class > ss.java

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java反编译工具jad 1.5.8g支持 jdk1.5,jdk1.6。说明很多记住一个万能的命令基本就够用了。jad -sjava -r -8 -o **\*.class ---------------This is README file for Jad - the fast Java Decompiler.Jad home page: http://www.kpdus.com/jad.htmlCopyright 2001 Pavel Kouznetsov (jad@kpdus.com).0. Please read the disclaimer on the Jad home page.1. Installation.Unzip jad.zip file into any appropriate directory on your hard drive.This will create two files: - an executable file named 'jad.exe' (Windows *) or 'jad' (*n*x) - this README fileNo other setup is required.2. How to use JadTo decompile a single JAVA class file 'example1.class' type the following: jad example1.classThis command creates file 'example1.jad' in the current directory.If such file already exists Jad asks whether you want to overwrite it or not.Option -o permits overwriting without a confirmation.You can omit .class extension and/or use wildcards in the names ofinput files.Option -s allows to change output file extension: jad -sjava example1.classThis command creates file 'example1.java'. Be careful when usingoptions -o and -sjava together, because Jad can accidentally overwriteyour own source files.Jad uses JAVA class name as an output file name. For example, if classfile 'example1.class' contains JAVA class 'test1' then Jad will createfile 'test1.jad' rather than 'example1.jad'. If you want to specifyyour own output file name use the output redirection: jad -p example1.class > myexm1.javaOption -d allows you to specify another directory for output files,which are created, by default, in the current directory. For example: jad -o -dtest -sjava *.class (or jad -o -d test -s java *.class, which has the same effect)This command decompiles all .class files in the current directory <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值