javap 是 Java 开发工具包(JDK)中的一个工具,用于反编译 Java 字节码文件,以便查看类的结构、方法、字段以及其他相关信息。通过 javap 命令,开发人员可以快速了解 Java 类的内部结构,包括类的成员、访问修饰符、方法签名等信息。
javap <options> <classes>
-help --help -? 输出此用法消息
-version 版本信息
-v -verbose 输出附加信息
-l 输出行号和本地变量表
-public 仅显示公共类和成员
-protected 显示受保护的/公共类和成员
-package 显示程序包/受保护的/公共类和成员 (默认)
-p -private 显示所有类和成员
-c 对代码进行反汇编
-s 输出内部类型签名
-sysinfo 显示正在处理的类的系统信息 (路径, 大小, 日期, MD5 散列)
-constants 显示最终常量
-classpath <path> 指定查找用户类文件的位置
-cp <path> 指定查找用户类文件的位置
-bootclasspath <path> 覆盖引导类文件的位置
(1)使用 javap classname 命令显示指定类的反编译信息。例如:
C:\Program Files\Java\jdk1.8.0_171\bin> javap Demo20231207193016.class Compiled from "Demo20231207193016.java" public class com.huangxin.demo202312.Demo20231207193016 { public com.huangxin.demo202312.Demo20231207193016(); public static void main(java.lang.String[]) throws java.lang.Exception; }
注意:我将 Demo20231207193016.class 字节码文件拷贝到了 javap 命令所在的目录了。
(2)使用 -c 选项显示反编译后的字节码指令。例如:
C:\Program Files\Java\jdk1.8.0_171\bin> javap -c Demo20231207193016.class Compiled from "Demo20231207193016.java" public class com.huangxin.demo202312.Demo20231207193016 { public com.huangxin.demo202312.Demo20231207193016(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]) throws java.lang.Exception; Code: 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 3: invokestatic #3 // Method java/lang/System.currentTimeMillis:()J 6: invokevirtual #4 // Method java/io/PrintStream.println:(J)V 9: ldc2_w #5 // long 1000l 12: invokestatic #7 // Method java/lang/Thread.sleep:(J)V 15: goto 0 }
(3)使用 -s 选项显示内部类型签名。例如:
C:\Program Files\Java\jdk1.8.0_171\bin> javap -s Demo20231207193016.class Compiled from "Demo20231207193016.java" public class com.huangxin.demo202312.Demo20231207193016 { public com.huangxin.demo202312.Demo20231207193016(); descriptor: ()V public static void main(java.lang.String[]) throws java.lang.Exception; descriptor: ([Ljava/lang/String;)V }
(4)使用 -verbose 选项显示详细的信息,包括类的构造器、方法、字段等。例如:
C:\Program Files\Java\jdk1.8.0_171\bin> javap -verbose Demo20231207193016.class Classfile /C:/Program Files/Java/jdk1.8.0_171/bin/Demo20231207193016.class Last modified 2023-12-7; size 738 bytes MD5 checksum 3ed0670d75604ce43ff4e500139a89ac Compiled from "Demo20231207193016.java" public class com.huangxin.demo202312.Demo20231207193016 minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #9.#26 // java/lang/Object."<init>":()V #2 = Fieldref #27.#28 // java/lang/System.out:Ljava/io/PrintStream; #3 = Methodref #27.#29 // java/lang/System.currentTimeMillis:()J #4 = Methodref #30.#31 // java/io/PrintStream.println:(J)V #5 = Long 1000l #7 = Methodref #32.#33 // java/lang/Thread.sleep:(J)V #8 = Class #34 // com/huangxin/demo202312/Demo20231207193016 #9 = Class #35 // java/lang/Object #10 = Utf8 <init> #11 = Utf8 ()V #12 = Utf8 Code #13 = Utf8 LineNumberTable #14 = Utf8 LocalVariableTable #15 = Utf8 this #16 = Utf8 Lcom/huangxin/demo202312/Demo20231207193016; #17 = Utf8 main #18 = Utf8 ([Ljava/lang/String;)V #19 = Utf8 args #20 = Utf8 [Ljava/lang/String; #21 = Utf8 StackMapTable #22 = Utf8 Exceptions #23 = Class #36 // java/lang/Exception #24 = Utf8 SourceFile #25 = Utf8 Demo20231207193016.java #26 = NameAndType #10:#11 // "<init>":()V #27 = Class #37 // java/lang/System #28 = NameAndType #38:#39 // out:Ljava/io/PrintStream; #29 = NameAndType #40:#41 // currentTimeMillis:()J #30 = Class #42 // java/io/PrintStream #31 = NameAndType #43:#44 // println:(J)V #32 = Class #45 // java/lang/Thread #33 = NameAndType #46:#44 // sleep:(J)V #34 = Utf8 com/huangxin/demo202312/Demo20231207193016 #35 = Utf8 java/lang/Object #36 = Utf8 java/lang/Exception #37 = Utf8 java/lang/System #38 = Utf8 out #39 = Utf8 Ljava/io/PrintStream; #40 = Utf8 currentTimeMillis #41 = Utf8 ()J #42 = Utf8 java/io/PrintStream #43 = Utf8 println #44 = Utf8 (J)V #45 = Utf8 java/lang/Thread #46 = Utf8 sleep { public com.huangxin.demo202312.Demo20231207193016(); descriptor: ()V flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 7: 0 LocalVariableTable: Start Length Slot Name Signature 0 5 0 this Lcom/huangxin/demo202312/Demo20231207193016; public static void main(java.lang.String[]) throws java.lang.Exception; descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=3, locals=1, args_size=1 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream; 3: invokestatic #3 // Method java/lang/System.currentTimeMillis:()J 6: invokevirtual #4 // Method java/io/PrintStream.println:(J)V 9: ldc2_w #5 // long 1000l 12: invokestatic #7 // Method java/lang/Thread.sleep:(J)V 15: goto 0 LineNumberTable: line 11: 0 line 12: 9 LocalVariableTable: Start Length Slot Name Signature 0 18 0 args [Ljava/lang/String; StackMapTable: number_of_entries = 1 frame_type = 0 /* same */ Exceptions: throws java.lang.Exception } SourceFile: "Demo20231207193016.java"
(5)使用 -classpath 选项指定 javap 工具查找类文件的路径。例如:
C:\Program Files\Java\jdk1.8.0_171\bin>javap -classpath E:\~hx_demo\target\classes com.huangxin.demo202312.Demo20231207193016 Compiled from "Demo20231207193016.java" public class com.huangxin.demo202312.Demo20231207193016 { public com.huangxin.demo202312.Demo20231207193016(); public static void main(java.lang.String[]) throws java.lang.Exception; }