javap
The javap tool is an essential utility in the Java Development Kit (JDK) used for disassembling class files. It provides insights into the compiled output of Java code by displaying method declarations, bytecode, and method signatures. This tool is particularly useful for debugging and understanding how Java code is transformed into bytecode by the Java compiler.
Basic Usage
The simplest form of using javap is to display the structure of a class file. For example, consider the following command:
$ javap LoadSomeClasses.class
This command outputs:
Compiled from "LoadSomeClasses.java"
public class LoadSomeClasses {
public LoadSomeClasses();
public static void main(java.lang.String[]);
}
This output shows the public constructor and the main method of the LoadSomeClasses class. If the class contains inner classes, they are compiled into separate class files, which can also be examined using javap:
$ javap LoadSomeClasses\$SadClassloader.class
This command outputs:
Compiled from "LoadSomeClasses.java"
public class LoadSomeClasses$SadClassloader extends java.lang.ClassLoader {
public LoadSomeClasses$SadClassloader();
public java.lang.Class<?> findClass(java.lang.String) throws
java.lang.ClassNotFoundException;
}
By default, javap displays methods with public, protected, and package-private access. To include private methods and fields in the output, the -p switch can be used.
Disassembling Bytecode
javap can also be used to disassemble bytecode, providing a detailed view of the instructions executed by the Java Virtual Machine (JVM). This is done using the -c option:
$ javap -c ch15/ExamplePrivate.class
The output for a class compiled from "ExamplePrivate.java" might look like this:
Compiled from "ExamplePrivate.java"
public class ch15.ExamplePrivate {
public ch15.ExamplePrivate();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public void entry();
Code:
0: aload_0
1: invokevirtual #2 // Method callThePrivate:()V #1
4: return
}
#1 This is now invokevirtual.
This output shows the bytecode instructions for the constructor and the entry method of the ExamplePrivate class. The invokespecial and invokevirtual instructions indicate how methods are called.
Changes in Method Invocation
The way private methods are invoked has changed in recent versions of Java. For instance, when compiled with Java 8, a private method might be called using invokespecial:
$ javap -c ch15/ExamplePrivate.class
Compiled from "ExamplePrivate.java"
public class ch15.ExamplePrivate {
public ch15.ExamplePrivate();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public void entry();
Code:
0: aload_0
1: invokespecial #2 // Method callThePrivate:()V
4: return
}
However, when recompiled with Java 11, the same method might be called using invokevirtual:
$ javap -c ch15/ExamplePrivate.class
Compiled from "ExamplePrivate.java"
public class ch15.ExamplePrivate {
public ch15.ExamplePrivate();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public void entry();
Code:
0: aload_0
1: invokevirtual #2 // Method callThePrivate:()V #1
4: return
}
#1 This is now invokevirtual.
This change reflects updates in the Java language and JVM specifications, particularly with the introduction of features like nestmates.
FAQ (Frequently asked questions)
What is the purpose of the 'javap' tool?
How does 'javap' assist developers?
How can you use 'javap' to view bytecode of a class?
What information does 'javap' provide about methods in a class?
What does the 'invokespecial' bytecode instruction do?