1.本质是jdk安装包bin目录下的javap
javap使用说明
直接在cmd命令行使用如下:
命令:javap -c xx.class
除了使用javap -c xx.class外,还可以使用javap -v xx.class。
相比于-c,-v会输出写附加信息,如下:
Compiled from "Demo1.java"
public class com.ztt.jvm.luban.Demo1
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #5.#27 // java/lang/Object."<init>":()V
#2 = Class #28 // com/ztt/jvm/luban/Demo1
#3 = Methodref #2.#27 // com/ztt/jvm/luban/Demo1."<init>":()V
#4 = Methodref #2.#29 // com/ztt/jvm/luban/Demo1.math:()I
#5 = Class #30 // java/lang/Object
#6 = Utf8 <init>
#7 = Utf8 ()V
#8 = Utf8 Code
#9 = Utf8 LineNumberTable
#10 = Utf8 LocalVariableTable
#11 = Utf8 this
#12 = Utf8 Lcom/ztt/jvm/luban/Demo1;
#13 = Utf8 main
#14 = Utf8 ([Ljava/lang/String;)V
#15 = Utf8 args
#16 = Utf8 [Ljava/lang/String;
#17 = Utf8 demo1
#18 = Utf8 MethodParameters
#19 = Utf8 math
#20 = Utf8 ()I
#21 = Utf8 a
#22 = Utf8 I
#23 = Utf8 b
#24 = Utf8 c
#25 = Utf8 SourceFile
#26 = Utf8 Demo1.java
#27 = NameAndType #6:#7 // "<init>":()V
#28 = Utf8 com/ztt/jvm/luban/Demo1
#29 = NameAndType #19:#20 // math:()I
#30 = Utf8 java/lang/Object
{
public com.ztt.jvm.luban.Demo1();
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 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/ztt/jvm/luban/Demo1;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=1
0: new #2 // class com/ztt/jvm/luban/Demo1
3: dup
4: invokespecial #3 // Method "<init>":()V
7: astore_1
8: aload_1
9: invokevirtual #4 // Method math:()I
12: pop
13: return
LineNumberTable:
line 6: 0
line 7: 8
line 8: 13
LocalVariableTable:
Start Length Slot Name Signature
0 14 0 args [Ljava/lang/String;
8 6 1 demo1 Lcom/ztt/jvm/luban/Demo1;
MethodParameters:
Name Flags
args
public int math();
descriptor: ()I
flags: ACC_PUBLIC
Code:
stack=2, locals=4, args_size=1
0: iconst_1
1: istore_1
2: iconst_2
3: istore_2
4: iload_1
5: iload_2
6: iadd
2.在idea中使用javap
个人觉得只要这种方式还是比较方便的,不需要其他配置,使用原生命令,还能熟悉命令行操作,个人推荐使用。
使用方法如下。
首先在target目录下找到需要反编译的.class文件,然后右键选择open in terminal
注意需要在class文件目录下,不要在java文件目录下。
然后,在命令行界面,执行命令,如下:
3.在IDEA下集成javap -v
上面那种方法效率只能说还是不高,其实可以在idea里建扩展工具建快捷方式
在tool setting 的Program输入工具的路径,这中间可以插入宏,比如$JDK_PATH$
,不需要自己再手动输入jdk的路径了。
在参数栏中输入-p $FileClass$
,$FileClass$
代表要解析的 class文件。
在Workding Directory中输入$OutputPath$
代表项目的输出路径
4.查看字节码使用idea自带的插件
这种方法比较简单,直接view菜单下就有,不过信息不全。
5.idea也可以安装jclasslib插件
jclasslib插件还是很方便的,setting里安装插件就行。
public class TestApp {
public static void main(String[] args) {
int a=0;
a=a++;
System.out.println(a);
a=++a;
System.out.println(a);
a+=2;
System.out.println(a);
}
}
输出:
字节码:
本地变量表: