javap定义

javap是 Java class文件分解器,可以反编译(即对javac编译的文件进行反编译),也可以查看java编译器生成的字节码。用于分解class文件。

测试类

  1. public class JavapTest {
  2. private static final int _P_1 = 1;
  3. public static final int _P_2 = 2;
  4. public static void main(String[] args) {
  5. int m = 0, n = 0;
  6. for (int i = 0; i < 10; i++) {
  7. m = m++;
  8. n = ++n;
  9. }
  10. System.out.println("m = " + m);
  11. System.out.println("n = " + n);
  12. }
  13. }

javap命令参数

  1. C:\Users\l>javap -help
  2. 用法: javap <options> <classes>
  3. 其中, 可能的选项包括:
  4. -? -h --help -help 输出此帮助消息
  5. -version 版本信息
  6. -v -verbose 输出附加信息
  7. -l 输出行号和本地变量表
  8. -public 仅显示公共类和成员
  9. -protected 显示受保护的/公共类和成员
  10. -package 显示程序包/受保护的/公共类
  11. 和成员 (默认)
  12. -p -private 显示所有类和成员
  13. -c 对代码进行反汇编
  14. -s 输出内部类型签名
  15. -sysinfo 显示正在处理的类的
  16. 系统信息 (路径, 大小, 日期, MD5 散列)
  17. -constants 显示最终常量
  18. --module <模块>, -m <模块> 指定包含要反汇编的类的模块
  19. --module-path <路径> 指定查找应用程序模块的位置
  20. --system <jdk> 指定查找系统模块的位置
  21. --class-path <路径> 指定查找用户类文件的位置
  22. -classpath <路径> 指定查找用户类文件的位置
  23. -cp <路径> 指定查找用户类文件的位置
  24. -bootclasspath <路径> 覆盖引导类文件的位置
  25. GNU 样式的选项可使用 = (而非空白) 来分隔选项名称
  26. 及其值。
  27. 每个类可由其文件名, URL 或其
  28. 全限定类名指定。示例:
  29. path/to/MyClass.class
  30. jar:file:///path/to/MyJar.jar!/mypkg/MyClass.class
  31. java.lang.Object

javap -version:显示java版本

  1. 11.0.14

javap -p:显示所有类和成员

  1. D:\Source\beike_one\JAVASE_02>javap -p JavapTest.class
  2. Compiled from "JavapTest.java"
  3. public class JavapTest {
  4. private static final int _P_1;
  5. public static final int _P_2;
  6. public JavapTest();
  7. public static void main(java.lang.String[]);

javap -public:仅显示公共类和成员

  1. D:\Source\beike_one\JAVASE_02>javap -public JavapTest.class
  2. Compiled from "JavapTest.java"
  3. public class JavapTest {
  4. public static final int _P_2;
  5. public JavapTest();
  6. public static void main(java.lang.String[]);
  7. }

javap -l:输出行号和本地变量表

  1. D:\Source\beike_one\JAVASE_02>javap -p -l JavapTest.class
  2. Compiled from "JavapTest.java"
  3. public class JavapTest {
  4. private static final int _P_1;
  5. public static final int _P_2;
  6. public JavapTest();
  7. LineNumberTable:
  8. line 1: 0
  9. public static void main(java.lang.String[]);
  10. LineNumberTable:
  11. line 7: 0
  12. line 8: 4
  13. line 9: 12
  14. line 10: 17
  15. line 8: 22
  16. line 12: 28
  17. line 13: 40
  18. line 14: 52
  19. }

javap -v/-p -v:

这是两个命令:
javap -v JavapTest.class
或者
javap -p -v JavapTest.class

命令说明是:
输出附加信息class文件的路径
最后修改时间
文件大小等类的全路径
源(java)文件等常量池常量定义
值构造方法程序调用及执行逻辑(这个涉及的内容就比较多了)

  1. D:\Source\beike_one\JAVASE_02>javap -p -v JavapTest.class
  2. Classfile /D:/Source/beike_one/JAVASE_02/JavapTest.class
  3. Last modified 202239日; size 1039 bytes
  4. MD5 checksum fa1231c69dc6eca6a29a533e2f027c0a
  5. Compiled from "JavapTest.java"
  6. public class JavapTest
  7. minor version: 0
  8. major version: 55
  9. flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  10. this_class: #6 // JavapTest
  11. super_class: #7 // java/lang/Object
  12. interfaces: 0, fields: 2, methods: 2, attributes: 3
  13. Constant pool:
  14. #1 = Methodref #7.#23 // java/lang/Object."<init>":()V
  15. #2 = Fieldref #24.#25 // java/lang/System.out:Ljava/io/PrintStream;
  16. #3 = InvokeDynamic #0:#29 // #0:makeConcatWithConstants:(I)Ljava/lang/String;
  17. #4 = Methodref #30.#31 // java/io/PrintStream.println:(Ljava/lang/String;)V
  18. #5 = InvokeDynamic #1:#29 // #1:makeConcatWithConstants:(I)Ljava/lang/String;
  19. #6 = Class #33 // JavapTest
  20. #7 = Class #34 // java/lang/Object
  21. #8 = Utf8 _P_1
  22. #9 = Utf8 I
  23. #10 = Utf8 ConstantValue
  24. #11 = Integer 1
  25. #12 = Utf8 _P_2
  26. #13 = Integer 2
  27. #14 = Utf8 <init>
  28. #15 = Utf8 ()V
  29. #16 = Utf8 Code
  30. #17 = Utf8 LineNumberTable
  31. #18 = Utf8 main
  32. #19 = Utf8 ([Ljava/lang/String;)V
  33. #20 = Utf8 StackMapTable
  34. #21 = Utf8 SourceFile
  35. #22 = Utf8 JavapTest.java
  36. #23 = NameAndType #14:#15 // "<init>":()V
  37. #24 = Class #35 // java/lang/System
  38. #25 = NameAndType #36:#37 // out:Ljava/io/PrintStream;
  39. #26 = Utf8 BootstrapMethods
  40. #27 = MethodHandle 6:#38 // REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  41. #28 = String #39 // m = \u0001
  42. #29 = NameAndType #40:#41 // makeConcatWithConstants:(I)Ljava/lang/String;
  43. #30 = Class #42 // java/io/PrintStream
  44. #31 = NameAndType #43:#44 // println:(Ljava/lang/String;)V
  45. #32 = String #45 // n = \u0001
  46. #33 = Utf8 JavapTest
  47. #34 = Utf8 java/lang/Object
  48. #35 = Utf8 java/lang/System
  49. #36 = Utf8 out
  50. #37 = Utf8 Ljava/io/PrintStream;
  51. #38 = Methodref #46.#47 // java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  52. #39 = Utf8 m = \u0001
  53. #40 = Utf8 makeConcatWithConstants
  54. #41 = Utf8 (I)Ljava/lang/String;
  55. #42 = Utf8 java/io/PrintStream
  56. #43 = Utf8 println
  57. #44 = Utf8 (Ljava/lang/String;)V
  58. #45 = Utf8 n = \u0001
  59. #46 = Class #48 // java/lang/invoke/StringConcatFactory
  60. #47 = NameAndType #40:#52 // makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  61. #48 = Utf8 java/lang/invoke/StringConcatFactory
  62. #49 = Class #54 // java/lang/invoke/MethodHandles$Lookup
  63. #50 = Utf8 Lookup
  64. #51 = Utf8 InnerClasses
  65. #52 = Utf8 (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  66. #53 = Class #55 // java/lang/invoke/MethodHandles
  67. #54 = Utf8 java/lang/invoke/MethodHandles$Lookup
  68. #55 = Utf8 java/lang/invoke/MethodHandles
  69. {
  70. private static final int _P_1;
  71. descriptor: I
  72. flags: (0x001a) ACC_PRIVATE, ACC_STATIC, ACC_FINAL
  73. ConstantValue: int 1
  74. public static final int _P_2;
  75. descriptor: I
  76. flags: (0x0019) ACC_PUBLIC, ACC_STATIC, ACC_FINAL
  77. ConstantValue: int 2
  78. public JavapTest();
  79. descriptor: ()V
  80. flags: (0x0001) ACC_PUBLIC
  81. Code:
  82. stack=1, locals=1, args_size=1
  83. 0: aload_0
  84. 1: invokespecial #1 // Method java/lang/Object."<init>":()V
  85. 4: return
  86. LineNumberTable:
  87. line 1: 0
  88. public static void main(java.lang.String[]);
  89. descriptor: ([Ljava/lang/String;)V
  90. flags: (0x0009) ACC_PUBLIC, ACC_STATIC
  91. Code:
  92. stack=2, locals=4, args_size=1
  93. 0: iconst_0
  94. 1: istore_1
  95. 2: iconst_0
  96. 3: istore_2
  97. 4: iconst_0
  98. 5: istore_3
  99. 6: iload_3
  100. 7: bipush 10
  101. 9: if_icmpge 28
  102. 12: iload_1
  103. 13: iinc 1, 1
  104. 16: istore_1
  105. 17: iinc 2, 1
  106. 20: iload_2
  107. 21: istore_2
  108. 22: iinc 3, 1
  109. 25: goto 6
  110. 28: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
  111. 31: iload_1
  112. 32: invokedynamic #3, 0 // InvokeDynamic #0:makeConcatWithConstants:(I)Ljava/lang/String;
  113. 37: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
  114. 40: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
  115. 43: iload_2
  116. 44: invokedynamic #5, 0 // InvokeDynamic #1:makeConcatWithConstants:(I)Ljava/lang/String;
  117. 49: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
  118. 52: return
  119. LineNumberTable:
  120. line 7: 0
  121. line 8: 4
  122. line 9: 12
  123. line 10: 17
  124. line 8: 22
  125. line 12: 28
  126. line 13: 40
  127. line 14: 52
  128. StackMapTable: number_of_entries = 2
  129. frame_type = 254 /* append */
  130. offset_delta = 6
  131. locals = [ int, int, int ]
  132. frame_type = 250 /* chop */
  133. offset_delta = 21
  134. }
  135. SourceFile: "JavapTest.java"
  136. InnerClasses:
  137. public static final #50= #49 of #53; // Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
  138. BootstrapMethods:
  139. 0: #27 REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  140. Method arguments:
  141. #28 m = \u0001
  142. 1: #27 REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
  143. Method arguments:
  144. #32 n = \u0001

javap -c:对代码进行反汇编

javap-c输出内部javap-v中已经有了,我们详细介绍下javap-c命令的输出内容0:iconst_0前面0:表示执行的顺序,iconst_0把值0放入栈顶,_0中的0代表压栈的值,如:iconst_5,即把5压入栈顶1:istore_1将栈顶的值放入变量1中,_1代表变量的序列,本例中为:m,如:istore_2即为变量n赋值6:iload_3将变量3即i的的值放入栈顶,与iconst不同的是,iload操作的值是已经定义好存在的值,iconst是定义时的压栈操作13:iinc1,1将变量1的值加1

  1. D:\Source\beike_one\JAVASE_02>javap -c JavapTest.class
  2. Compiled from "JavapTest.java"
  3. public class JavapTest {
  4. public static final int _P_2;
  5. public JavapTest();
  6. Code:
  7. 0: aload_0
  8. 1: invokespecial #1 // Method java/lang/Object."<init>":()V
  9. 4: return
  10. public static void main(java.lang.String[]);
  11. Code:
  12. 0: iconst_0
  13. 1: istore_1
  14. 2: iconst_0
  15. 3: istore_2
  16. 4: iconst_0
  17. 5: istore_3
  18. 6: iload_3
  19. 7: bipush 10
  20. 9: if_icmpge 28
  21. 12: iload_1
  22. 13: iinc 1, 1
  23. 16: istore_1
  24. 17: iinc 2, 1
  25. 20: iload_2
  26. 21: istore_2
  27. 22: iinc 3, 1
  28. 25: goto 6
  29. 28: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
  30. 31: iload_1
  31. 32: invokedynamic #3, 0 // InvokeDynamic #0:makeConcatWithConstants:(I)Ljava/lang/String;
  32. 37: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
  33. 40: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
  34. 43: iload_2
  35. 44: invokedynamic #5, 0 // InvokeDynamic #1:makeConcatWithConstants:(I)Ljava/lang/String;
  36. 49: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
  37. 52: return
  38. }