Understanding javap tool

javap tool in java, javap swtiches, listing classes, methods and constructor in java, reflection in java, Class class, creating appletviewer tool, accessing private method etc.

Understanding javap tool

Understanding javap tool

The javap command disassembles a class file. The javap command displays information about the fields, constructors and methods present in a class file.

Syntax to use javap tool

Let's see how to use javap tool or command.

  1. javap fully_class_name  

Example to use javap tool

  1. javap java.lang.Object  

Output:

 

  1. Compiled from "Object.java"  
  2. public class java.lang.Object {  
  3.   public java.lang.Object();  
  4.   public final native java.lang.Class<?> getClass();  
  5.   public native int hashCode();  
  6.   public boolean equals(java.lang.Object);  
  7.   protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;  
  8.   public java.lang.String toString();  
  9.   public final native void notify();  
  10.   public final native void notifyAll();  
  11.   public final native void wait(longthrows java.lang.InterruptedException;  
  12.   public final void wait(longintthrows java.lang.InterruptedException;  
  13.   public final void wait() throws java.lang.InterruptedException;  
  14.   protected void finalize() throws java.lang.Throwable;  
  15.   static {};  
  16. }  

Another example to use javap tool for your class

Let's use the javap command for our java file.

  1. class Simple{  
  2. public static void main(String args[]){  
  3. System.out.println("hello java");  
  4. }  
  5. }  

Now let's use the javap tool to disassemble the class file.

  1. javap Simple  

Output:

 

  1. Compiled from ".java"  
  2. class Simple {  
  3.   Simple();  
  4.   public static void main(java.lang.String[]);  
  5. }  

javap -c command

You can use the javap -c command to see disassembled code. The code that reflects the java bytecode.

  1. javap -c Simple  

Output:

 

  1. Compiled from ".java"  
  2. class Simple {  
  3.   Simple();  
  4.     Code:  
  5.        0: aload_0         
  6.        1: invokespecial #1                  // Method java/lang/Object."<init>":()V  
  7.        4return          
  8.   
  9.   public static void main(java.lang.String[]);  
  10.     Code:  
  11.        0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;  
  12.        3: ldc           #3                  // String hello java  
  13.        5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V  
  14.        8return          
  15. }  

Options of javap tool

The important options of javap tool are as follows.

Option Description
-help prints the help message.
-l prints line number and local variable
-c disassembles the code
-s prints internal type signature
-sysinfo shows system info (path, size, date, MD5 hash)
-constants shows static final constants
-version shows version information