javap 反编译工具

javap是JDK自带的反汇编工具,可以将.class字节码文件反汇编为JVM指令,或是反编译为Java源代码。我们可以使用javap -help命令查看可用的命令行参数,例子输出如下。

Usage: javap <options> <classes>
where possible options include:
  -? -h --help -help               Print this help message
  -version                         Version information
  -v  -verbose                     Print additional information
  -l                               Print line number and local variable tables
  -public                          Show only public classes and members
  -protected                       Show protected/public classes and members
  -package                         Show package/protected/public classes
                                   and members (default)
  -p  -private                     Show all classes and members
  -c                               Disassemble the code
  -s                               Print internal type signatures
  -sysinfo                         Show system info (path, size, date, MD5 hash)
                                   of class being processed
  -constants                       Show final constants
  --module <module>, -m <module>   Specify module containing classes to be disassembled
  --module-path <path>             Specify where to find application modules
  --system <jdk>                   Specify where to find system modules
  --class-path <path>              Specify where to find user class files
  -classpath <path>                Specify where to find user class files
  -cp <path>                       Specify where to find user class files
  -bootclasspath <path>            Override location of bootstrap class files

GNU-style options may use = instead of whitespace to separate the name of an option
from its value.

Each class to be shown may be specified by a filename, a URL, or by its fully
qualified class name. Examples:
   path/to/MyClass.class
   jar:file:///path/to/MyJar.jar!/mypkg/MyClass.class
   java.lang.Object

反编译class文件

如果想要查看一个.class字节码文件的源代码,我们一般使用以下命令进行反编译操作。

javap -private <class文件名>

其中,-private参数用于显示private权限的方式,不加该参数默认的不会显示的。

查看class文件中的指令代码

使用-v参数可以查看.class文件中的常量池、局部变量表、编译后的JVM指令等信息。

javap -private -v <class文件名>

实际上,如果是开发阶段的非命令行环境,建议还是使用jclasslib等图形化反汇编工具,比起命令行要方便很多。

作者:Gacfox
版权声明:本网站为非盈利性质,文章如非特殊说明均为原创,版权遵循知识共享协议CC BY-NC-ND 4.0进行授权,转载必须署名,禁止用于商业目的或演绎修改后转载。