Common commands for Java compilation - javap

javap defines
javap is a Java class file decomposer, which can be decompiled (that is, decompiled files compiled by javac), and can also view the bytecode generated by the java compiler. Used to decompose class files.
test class
public class JavapTest {

    private static final int _P_1 = 1;
    public static final int _P_2 = 2;

    public static void main(String[] args) {
        int m = 0, n = 0;
        for (int i = 0; i < 10; i++) {
            m = m++;
            n = ++n;
        }
        System.out.println("m = " + m);
        System.out.println("n = " + n);
    }
}

javap command parameters
C:\>javap -help
usage: javap <options> <classes>
Among them, possible options include:
-help --help -? print this usage message
  -version version information
  -v -verbose output additional information
  -l output line numbers and local variable table
  -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 output internal type signature
  -sysinfo shows the class's
                           System info (path, size, date, MD5 hash)
  -constants show static final constants
  -classpath <path> specifies where to look for user class files
  -bootclasspath <path> Override the location of the boot class file

javap -version
1.7.0_71

show java version

javap -p
C:\Users\user\Desktop>javap -p JavapTest.class
Compiled from "JavapTest.java"
public class com.method.handler.JavapTest {
  private static final int _P_1;
  public static final int _P_2;
  public com.method.handler.JavapTest();
  public static void main(java.lang.String[]);
  private void say();
}


Displays all members of the accessible modifier scope "private" of the class

javap -public
C:\Users\user\Desktop>javap -public JavapTest.class
Compiled from "JavapTest.java"
public class com.method.handler.JavapTest {
  public static final int _P_2;
  public com.method.handler.JavapTest();
  public static void main(java.lang.String[]);
}


show the public members of the class

javap -protected
C:\Users\user\Desktop>javap -protected JavapTest.class
Compiled from "JavapTest.java"
public class com.method.handler.JavapTest {
  public static final int _P_2;
  public com.method.handler.JavapTest();
  public static void main(java.lang.String[]);
}

Displays all members of the class that are accessible by modifier scope > protected

javap-l
C:\Users\user\Desktop>javap -p -l JavapTest.class
Compiled from "JavapTest.java"
public class com.method.handler.JavapTest {
  private static final int _P_1;

  public static final int _P_2;

  public com.method.handler.JavapTest();
    LineNumberTable:
      line 6: 0

  public static void main(java.lang.String[]);
    LineNumberTable:
      line 12: 0
      line 13: 4
      line 14: 12
      line 15: 17
      line 13: 22
      line 17: 28
      line 18: 53
      line 19: 78

  private void say();
    LineNumberTable:
      line 23: 0
      line 24: 8
}


Output line numbers and local variable table? I don't understand

javap -package
C:\Users\user\Desktop>javap -package JavapTest.class
Compiled from "JavapTest.java"
public class com.method.handler.JavapTest {
  public static final int _P_2;
  public com.method.handler.JavapTest();
  public static void main(java.lang.String[]);
}


Similar to javap -public

javap -v/-p -v
C:\Users\user\Desktop>javap -p -v JavapTest.class
Classfile /C:/Users/frinder_liu/Desktop/JavapTest.class
  Last modified 2016-4-27; size 911 bytes
  MD5 checksum e903be7495f5c462d6459a792e063628
  Compiled from "JavapTest.java"
public class com.method.handler.JavapTest
  SourceFile: "JavapTest.java"
  minor version: 0
  major version: 51
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #13.#30        //  java/lang/Object."<init>":()V
   #2 = Fieldref           #31.#32        //  java/lang/System.out:Ljava/io/PrintStream;
   #3 = Class              #33            //  java/lang/StringBuilder
   #4 = Methodref          #3.#30         //  java/lang/StringBuilder."<init>":()V
   #5 = String             #34            //  m =
   #6 = Methodref          #3.#35         //  java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilde
r;
   #7 = Methodref          #3.#36         //  java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
   #8 = Methodref          #3.#37         //  java/lang/StringBuilder.toString:()Ljava/lang/String;
   #9 = Methodref          #38.#39        //  java/io/PrintStream.println:(Ljava/lang/String;)V
  #10 = String             #40            //  n =
  #11 = String             #41            //  hello world...
  #12 = Class              #42            //  com/method/handler/JavapTest
  #13 = Class              #43            //  java/lang/Object
  #14 = Utf8               _P_1
  #15 = Utf8               I
  #16 = Utf8               ConstantValue
  #17 = Integer            1
  #18 = Utf8               _P_2
  #19 = Integer            2
  #20 = Utf8               <init>
  #21 = Utf8()V
  #22 = Utf8               Code
  #23 = Utf8               LineNumberTable
  #24 = Utf8               main
  #25 = Utf8               ([Ljava/lang/String;)V
  # 26 = Utf8 StackMapTable
  #27 = Utf8               say
  #28 = Utf8               SourceFile
  #29 = Utf8 JavapTest.java
  #30 = NameAndType        #20:#21        //  "<init>":()V
  #31 = Class              #44            //  java/lang/System
  #32 = NameAndType        #45:#46        //  out:Ljava/io/PrintStream;
  #33 = Utf8               java/lang/StringBuilder
  #34 = Utf8               m =
  #35 = NameAndType        #47:#48        //  append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  #36 = NameAndType        #47:#49        //  append:(I)Ljava/lang/StringBuilder;
  #37 = NameAndType        #50:#51        //  toString:()Ljava/lang/String;
  #38 = Class              #52            //  java/io/PrintStream
  #39 = NameAndType        #53:#54        //  println:(Ljava/lang/String;)V
  #40 = Utf8               n =
  #41 = Utf8               hello world...
  #42 = Utf8               com/method/handler/JavapTest
  #43 = Utf8               java/lang/Object
  #44 = Utf8               java/lang/System
  #45 = Utf8               out
  #46 = Utf8               Ljava/io/PrintStream;
  #47 = Utf8               append
  # 48 = Utf8 (Ljava / lang / String;) Ljava / lang / StringBuilder;
  #49 = Utf8               (I)Ljava/lang/StringBuilder;
  #50 = Utf8               toString
  #51 = Utf8               ()Ljava/lang/String;
  #52 = Utf8               java/io/PrintStream
  #53 = Utf8               println
  #54 = Utf8               (Ljava/lang/String;)V
{
  private static final int _P_1;
    flags: ACC_PRIVATE, ACC_STATIC, ACC_FINAL
    ConstantValue: int 1

  public static final int _P_2;
    flags: ACC_PUBLIC, ACC_STATIC, ACC_FINAL
    ConstantValue: int 2

  public com.method.handler.JavapTest();
    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 6: 0

  public static void main(java.lang.String[]);
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=3, locals=4, args_size=1
         0: iconst_0
         1: istore_1
         2: iconst_0
         3: istore_2
         4: iconst_0
         5: istore_3
         6: iload_3
         7: bipush 10
         9: if_icmpge     28
        12: iload_1
        13: verse 1, 1
        16: istore_1
        17: here 2, 1
        20: iload_2
        21: istore_2
        22: here 3, 1
        25: goto          6
        28: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
        31: new           #3                  // class java/lang/StringBuilder
        34: dup
        35: invokespecial #4                  // Method java/lang/StringBuilder."<init>":()V
        38: ldc           #5                  // String m =
        40: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/St
ringBuilder;
        43: iload_1
        44: invokevirtual #7                  // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
        47: invokevirtual #8                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        50: invokevirtual #9                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        53: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
        56: new           #3                  // class java/lang/StringBuilder
        59: dup
        60: invokespecial #4                  // Method java/lang/StringBuilder."<init>":()V
        63: ldc           #10                 // String n =
        65: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/St
ringBuilder;
        68: iload_2
        69: invokevirtual #7                  // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
        72: invokevirtual #8                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
        75: invokevirtual #9                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        78: return
      LineNumberTable:
        line 12: 0
        line 13: 4
        line 14: 12
        line 15: 17
        line 13: 22
        line 17: 28
        line 18: 53
        line 19: 78
      StackMapTable: number_of_entries = 2
           frame_type = 254 /* append */
             offset_delta = 6
        locals = [ int, int, int ]
           frame_type = 250 /* chop */
          offset_delta = 21


  private void say();
    flags: ACC_PRIVATE
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #11                 // String hello world...
         5: invokevirtual #9                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 23: 0
        line 24: 8
}

The command description is: output additional information
class file path, last modification time, file size, etc. full path of
class , source (java) file, etc.
constant pool
constant definition, value
construction method
program call and execution logic (the content involved is compared More)

In short, the javap -v command is a very powerful command!
javap -c
C:\Users\user\Desktop>javap -c JavapTest.class
Compiled from "JavapTest.java"
public class com.method.handler.JavapTest {
  public static final int _P_2;

  public com.method.handler.JavapTest();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_0
       1: istore_1
       2: iconst_0
       3: istore_2
       4: iconst_0
       5: istore_3
       6: iload_3
       7: bipush 10
       9: if_icmpge     28
      12: iload_1
      13: verse 1, 1
      16: istore_1
      17: here 2, 1
      20: iload_2
      21: istore_2
      22: here 3, 1
      25: goto          6
      28: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      31: new           #3                  // class java/lang/StringBuilder
      34: dup
      35: invokespecial #4                  // Method java/lang/StringBuilder."<init>":()V
      38: ldc           #5                  // String m =
      40: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/Stri
ngBuilder;
      43: iload_1
      44: invokevirtual #7                  // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      47: invokevirtual #8                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      50: invokevirtual #9                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      53: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      56: new           #3                  // class java/lang/StringBuilder
      59: dup
      60: invokespecial #4                  // Method java/lang/StringBuilder."<init>":()V
      63: ldc           #10                 // String n =
      65: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/Stri
ngBuilder;
      68: iload_2
      69: invokevirtual #7                  // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      72: invokevirtual #8                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      75: invokevirtual #9                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      78: return
}

In fact, the output of javap -c already exists in javap -v. We will introduce the output content of the javap -c command in detail.
0: iconst_0 The front 0: indicates the order of execution, icont_0 puts the value 0 on the top of the stack, 0 in _0 Represents the value pushed on the stack, such as: icont_5, that is, push 5 to the top of the stack
1: istore_1 Put the value of the top of the stack into variable 1, _1 represents the sequence of variables, in this case: m, such as: istore_2 is the variable n assignment
6: iload_3 puts the value of variable 3, i.e. i, on the top of the stack. Different from icont, the value of iload operation is the value that has already been defined, and icont is the push operation when it is defined.
13: iinc 1, 1 What is the final result of adding 1 to the value of variable 1 in
the demo at the beginning of the article?

m = 0
n = 10

why?
Let's focus on these lines:
12: iload_1 -- put the value of variable 1 on the stack item
13: iinc 1, 1 -- increment variable 1 by 1 to become 2, the top value of the stack is still 1. where the first 1 represents a variable and the second 1 represents an increment of
16: istore_1 -- assign the top value of the stack to variable 1, variable 1 is still 1
17: iinc 2, 1 -- increment variable 2 by 1 to become 2 , the top value of the stack is still 1. The first 2 represents the variable, and the second 1 represents an increment of
20: iload_2 -- put the value of variable 2 on the top of the stack
21: istore_2 -- assign the top value of the stack to variable 2, and the value of variable 2 is 2.


Remember a point:
int m = 0, n = 0;
m = m++; -- will first assign the m value (ie 0) to m and then ++
n = ++n; -- n first ++ and then assign the value to n
At this time, m = 0, n = 1;
this place is a bit confusing, you need to read it a few more times, straighten out the logic and relation!

Here you can refer to http://blog.csdn.net/junsure2012/article/details/7099222, which is very detailed and easy to understand!


javap -s/-p -s
C:\Users\user\Desktop>javap -p -s JavapTest.class
Compiled from "JavapTest.java"
public class com.method.handler.JavapTest {
  private static final int _P_1;
    Signature: I
  public static final int _P_2;
    Signature: I
  public com.method.handler.JavapTest();
    Signature: ()V

  public static void main(java.lang.String[]);
    Signature: ([Ljava/lang/String;)V

  private void say();
    Signature: ()V
}

output internal type signature

javap -sysinfo/-p -sysinfo
C:\Users\user\Desktop>javap -sysinfo JavapTest.class
Classfile /C:/Users/user/Desktop/JavapTest.class
  Last modified 2016-4-27; size 911 bytes
  MD5 checksum 3f6dfcf7121785760b234224c5d135fd
  Compiled from "JavapTest.java"
public class com.method.handler.JavapTest {
  public static final int _P_2;
  public com.method.handler.JavapTest();
  public static void main(java.lang.String[]);
}


Display
system (path, size, date, MD5 hash)

javap -constants/-p -constants
C:\Users\user\Desktop>javap -constants JavapTest.class
Compiled from "JavapTest.java"
public class com.method.handler.JavapTest {
  public static final int _P_2 = 2;
  public com.method.handler.JavapTest();
  public static void main(java.lang.String[]);
}


show static final constants

javap -classpath/-bootclasspath

skip first

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326998684&siteId=291194637