Introduction to Java decompilation tool javap

1. Decompilation definition

Java decompilation refers to the process of converting Java program files (.class files) that have been compiled into bytecodes back to source code (.java files).

2. Decompilation usage scenarios

1. Code review and debugging:

]]When we need to review or debug a compiled Java program, decompilation can help us restore the source code, making it easier for us to analyze and debug.

2. Study and research:

Through decompilation, we can learn and study Java programs written by others, understand their implementation principles and design ideas, and gain experience and inspiration from them.

3. Reverse engineering:

Sometimes we need to reverse engineer a compiled Java program, such as finding vulnerabilities in the program, modifying the behavior of the program, or performing modifications, customizations, etc.

4. Code reuse:

When we need to use part of the code in a compiled Java program, decompilation can help us restore the source code, making it easier for us to reuse and modify it.

It should be noted that decompilation is limited to non-commercial and legal uses. In a commercial environment, decompiling other people's code may involve infringement and illegal activities, so it needs to be used with caution.

3. Decompilation tool javap

1. Introduction to javap:

javap is a Java decompilation tool used to view the bytecode instructions of Java classes.

It can display information such as member variables, methods, constructors, annotations, and constant pools of the class.

The basic syntax for using the javap command is as follows:

javap [options] [classes]

Among them, options are optional parameters used to specify some options, and classes are the names of the Java classes to be viewed.

Commonly used options include:

  • -c: Display the bytecode instructions of the class.
  • -s: Display the signature information of the class.
  • -l: Display line numbers and local variable tables.
  • -verbose: Display additional information such as version number and constant pool.
    For example, to view the bytecode instructions for a Java class named MyClass, you can use the following command:
javap -c MyClass.class

This displays the bytecode instructions for the MyClass class.

In addition to the basic options, javap also provides some other advanced options, you can check the official documentation for more detailed information.

2. Case demonstration:

Suppose there is a Java class named MyClass with the following code:

public class MyClass {
    
    
    private String name;
    public int age;

    public MyClass(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }

    public void sayHello() {
    
    
        System.out.println("Hello, " + name + "!");
    }
}

Use the javap command to view the bytecode instructions of this class. You can execute the following command:

javap -c MyClass.class

as the picture shows:

javap

The output is as follows:

public class MyClass {
    
    
  public java.lang.String name;
  public int age;
  public MyClass(java.lang.String, int);
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: aload_1
       6: putfield      #2                  // Field name:Ljava/lang/String;
       9: aload_0
      10: iload_2
      11: putfield      #3                  // Field age:I
      14: return

  public void sayHello();
    Code:
       0: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: new           #5                  // class java/lang/StringBuilder
       6: dup
       7: ldc           #6                  // String Hello, 
       9: invokespecial #7                  // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
      12: aload_0
      13: getfield      #2                  // Field name:Ljava/lang/String;
      16: invokevirtual #8                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      19: ldc           #9                  // String !
      21: invokevirtual #8                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      24: invokevirtual #10                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      27: invokevirtual #11                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      30: return
}

As you can see, by using the -c option of the javap command, we can view the constructor of the MyClass class and the bytecode instructions of the sayHello method. This can help us better understand the underlying implementation of Java code.

Guess you like

Origin blog.csdn.net/qq_39939541/article/details/131777368