Understanding javap tool

The javap tool is a command-line utility that comes with the Java Development Kit (JDK). It is used to disassemble and inspect the bytecode of Java class files.

When you compile a Java source file, the Java compiler generates a class file containing the bytecode instructions that the Java Virtual Machine (JVM) will execute. The javap tool can be used to display the contents of this class file in a human-readable format.

Here’s how you can use the javap tool:

  1. Open a command prompt or terminal window.
  2. Navigate to the directory containing the class file you want to inspect.
  3. Type the following command:
javap -c <classname>

Replace <classname> with the name of the class you want to inspect.

The -c option tells javap to display the bytecode instructions in the disassembled output.

4. Press Enter to run the command.

javap will display the disassembled output for the specified class file. You’ll see a list of the class’s methods, along with their bytecode instructions.

Here’s an example output for a simple Java class:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}
$ javap -c HelloWorld
Compiled from "HelloWorld.java"
public class HelloWorld {
  public HelloWorld();
  public static void main(java.lang.String[]);
}
  1. This output shows that the HelloWorld class has two methods: a constructor and a main method that takes an array of strings as an argument. The bytecode instructions for these methods are not shown in this example.

In addition to the -c option, javap supports several other options that control the output format and level of detail. You can use the -help option to see a list of all the available options.

Overall, the javap tool is a useful utility for inspecting Java class files and understanding the bytecode instructions that the JVM will execute.

Syntax to use javap tool:

The syntax to use the javap tool is as follows:

javap [options] <class name>

Here, <class name> is the name of the class whose bytecode you want to inspect.

The javap tool supports several options, which control the output format and level of detail. Some of the commonly used options are:

  • -c: Display the bytecode instructions for each method.
  • -s: Display internal type signatures for each method.
  • -l: Display line numbers and local variable tables for each method.
  • -verbose: Display additional information about the class, such as its superclass and implemented interfaces.

For example, if you want to inspect the bytecode of the MyClass class and display the bytecode instructions for each method, you would use the following command:

javap -c MyClass

This would display the bytecode instructions for each method in the MyClass class.

Note that the javap tool should be run on the compiled .class file, not the original .java source file. Also, the class you want to inspect should be in the classpath, either in the current directory or in one of the directories listed in the CLASSPATH environment variable.

Example to use javap tool:

Sure, here’s an example of how to use the javap tool to inspect the bytecode of a simple Java class.

Let’s say we have the following Java class:

public class MyClass {
    private int value;

    public MyClass(int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

To inspect the bytecode of this class using the javap tool, follow these steps:

  1. Save the above code in a file named MyClass.java.
  2. Compile the MyClass.java file using the javac command. This will generate a MyClass.class file in the same directory.
javac MyClass.java

3. Use the javap tool to inspect the bytecode of the MyClass class. To display the bytecode instructions for each method, use the -c option.

javap -c MyClass

This will display the following output:

Compiled from "MyClass.java"
public class MyClass {
  private int value;
  public MyClass(int);
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iload_1
       6: putfield      #2                  // Field value:I
       9: return

  public int getValue();
    Code:
       0: aload_0
       1: getfield      #2                  // Field value:I
       4: ireturn

  public void setValue(int);
    Code:
       0: aload_0
       1: iload_1
       2: putfield      #2                  // Field value:I
       5: return
}

This output shows the bytecode instructions for each method in the MyClass class. For example, the bytecode instructions for the getValue method are:

public int getValue();
  Code:
     0: aload_0
     1: getfield      #2                  // Field value:I
     4: ireturn

These instructions load the this reference onto the stack, then load the value of the value field onto the stack, and finally return that value.

That’s it! This is how you can use the javap tool to inspect the bytecode of a Java class.

javap -c command:

The javap -c command is used to display the bytecode instructions for each method in a Java class.

For example, suppose we have a simple Java class named MyClass:

public class MyClass {
    public int add(int a, int b) {
        return a + b;
    }
}

If we compile this class and run the javap -c command on it:

javac MyClass.java
javap -c MyClass

We will see the following output:

Compiled from "MyClass.java"
public class MyClass {
  public MyClass();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public int add(int, int);
    Code:
       0: iload_1
       1: iload_2
       2: iadd
       3: ireturn
}

Here, the -c option has caused javap to display the bytecode instructions for each method in the class. For the add method, the bytecode instructions are:

public int add(int, int);
  Code:
     0: iload_1
     1: iload_2
     2: iadd
     3: ireturn

These instructions correspond to the return a + b; statement in the original Java code.

Specifically, the instructions load the values of a and b onto the operand stack (using the iload_1 and iload_2 instructions, respectively), add them together (using the iadd instruction), and then return the result (using the ireturn instruction).

By using the -c option with javap, we can get a detailed look at the bytecode instructions that a Java program executes, which can be useful for understanding how the program works under the hood.

Options of javap tool:

The javap tool is a command-line utility that is used to display information about the fields, methods, and bytecode of a Java class. The tool comes with several options that you can use to customize the output. Here are some of the most commonly used options:

  • -c: Prints the bytecode instructions for each method.
  • -s: Prints the internal signature of each method.
  • -verbose: Prints additional information about the class, such as the source file and the constant pool.
  • -classpath <path>: Specifies the path where Java class files are located.
  • -private: Shows all the private members of the class.
  • -protected: Shows all the protected members of the class.
  • -package: Shows all the members with package-level access (default).
  • -public: Shows all the public members of the class (default).
  • -l: Displays line numbers and local variable information.
  • -constants: Prints the contents of the constant pool.
  • -version: Displays the version of the Java Virtual Machine that will run the class.

For example, to display the bytecode instructions and internal signature of each method in a class named MyClass, you can run the following command:

javap -c -s MyClass

This will display the bytecode instructions and internal signature of each method in the MyClass class.

Note that the options you can use with the javap tool may vary depending on the version of the JDK you are using. You can see a full list of options by running the javap -help command.