java查看class文件字节码

Java

2017-06-27

49

0

技术:java8

运行环境:jdk8 + windows 7

demo功能:提供一个查看class文件字节码的方式

查看main.class文件的字节码信息

在main.class 所在目录里,打开命令行,javap -verbose main

源代码

public class main {
    public static void main(String[] args) {
        int n = 0;
        n++;
        System.out.println(n);
    }
}

 

字节码结果如下

F:\tmp\demoworld\demo-world\src\solr\target\test-classes\com\test>javap -verbose main
警告: 二进制文件main包含com.test.main
Classfile /F:/tmp/demoworld/demo-world/src/solr/target/test-classes/com/test/main.class
  Last modified 2017-6-27; size 529 bytes
  MD5 checksum bf1af82b90cfe51b40a4af4280e0c3a4
  Compiled from "main.java"
public class com.test.main
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #5.#21         // java/lang/Object."":()V
   #2 = Fieldref           #22.#23        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = Methodref          #24.#25        // java/io/PrintStream.println:(I)V
   #4 = Class              #26            // com/test/main
   #5 = Class              #27            // java/lang/Object
   #6 = Utf8               
   #7 = Utf8               ()V
   #8 = Utf8               Code
   #9 = Utf8               LineNumberTable
  #10 = Utf8               LocalVariableTable
  #11 = Utf8               this
  #12 = Utf8               Lcom/test/main;
  #13 = Utf8               main
  #14 = Utf8               ([Ljava/lang/String;)V
  #15 = Utf8               args
  #16 = Utf8               [Ljava/lang/String;
  #17 = Utf8               n
  #18 = Utf8               I
  #19 = Utf8               SourceFile
  #20 = Utf8               main.java
  #21 = NameAndType        #6:#7          // "":()V
  #22 = Class              #28            // java/lang/System
  #23 = NameAndType        #29:#30        // out:Ljava/io/PrintStream;
  #24 = Class              #31            // java/io/PrintStream
  #25 = NameAndType        #32:#33        // println:(I)V
  #26 = Utf8               com/test/main
  #27 = Utf8               java/lang/Object
  #28 = Utf8               java/lang/System
  #29 = Utf8               out
  #30 = Utf8               Ljava/io/PrintStream;
  #31 = Utf8               java/io/PrintStream
  #32 = Utf8               println
  #33 = Utf8               (I)V
{
  public com.test.main();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."":()V
         4: return
      LineNumberTable:
        line 6: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/test/main;

  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: iconst_0
         1: istore_1
         2: iinc          1, 1
         5: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         8: iload_1
         9: invokevirtual #3                  // Method java/io/PrintStream.println:(I)V
        12: return
      LineNumberTable:
        line 8: 0
        line 9: 2
        line 10: 5
        line 11: 12
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      13     0  args   [Ljava/lang/String;
            2      11     1     n   I
}
SourceFile: "main.java"

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think