How to open .class file in Java

javap is the Java Disassembler tool which can be used to open a .class file in a readable format. javap is located in the /bin folder of the JDK installation directory. The Java Decomplier (javap) displays information about the package, protected and public fields, and methods of the classes passed to it. The javap tool prints its output to stdout. In this article lets learn how to open a .class file in java with Example using javap.

Syntax for Javap

javap <<option>> <<.class file1>> <<.class file2>>

Javap Command Example in Java

lets use the Java Decomplier (Javap tool) to decompile our Demo class.

Demo.java

package com.javainterviewpoint;

public class Demo 
{
    public void disp()
    {
        System.out.println("Welcome to JavaInterviewPoint!!!");
    }
    public static void main(String args[])
    {
        Demo d1 = new Demo();
        d1.disp();
    }
}

Output :

upon executing javap command in cmd

javap com.javainterviewpoint.Demo

The below output will be produced

C:\JIP>javap com.javainterviewpoint.Demo
Compiled from "Demo.java"
public class com.javainterviewpoint.Demo {
  public com.javainterviewpoint.Demo();
  public void disp();
  public static void main(java.lang.String[]);
}

In the above code we have not used any options along with javap command in cmd, the most popular option is ‘-c’, this option prints instructions that comprise the Java bytecodes for each of the methods in the class.

C:\JIP>javap -c com.javainterviewpoint.Demo
Compiled from "Demo.java"
public class com.javainterviewpoint.Demo {
  public com.javainterviewpoint.Demo();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return

  public void disp();
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3                  // String Welcome to JavaInterviewPoint!!!
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #5                  // class com/javainterviewpoint/Demo
       3: dup
       4: invokespecial #6                  // Method "":()V
       7: astore_1
       8: aload_1
       9: invokevirtual #7                  // Method disp:()V
      12: return
}

How to use Javap command in Eclipse

Running javap command in eclipse can be done using External Tool Configuration only.  Goto “Run –> External Tools –> External Tools Configurations” as shown in below image.

Eclipse External Tools Configuration

Enter the below information in the new Configuration window

  1. Click on Program and click on New
  2. Give the name for the configuration, here we have given ‘javap -verbose’
  3. Select the Location of javap.exe, by browsing the file system. Usually javap.exe will be location in the <<jdk installation>>/bin directory
  4. Select the Working Directory, the location where the class file resides (Demo Class). Click on browse workspace under bin select the package under where Demo class resides (Here com/javainterviewpoint)
  5. In the Arguments block give -verbose Demo , where -verbose is the option which you want to execute along with javap and Demo is our class name.
  6. Click on Apply and Finally Run.

javap command eclipse configuration

We will get the below out displayed

Classfile /C:/Jackson/EclipseKepler/Workspace/MyJava/bin/com/javainterviewpoint/Demo.class
  Last modified Feb 5, 2016; size 696 bytes
  MD5 checksum ed681f968119b07f83eadbed004dcbee
  Compiled from "Demo.java"
public class com.javainterviewpoint.Demo
  SourceFile: "Demo.java"
  minor version: 0
  major version: 51
  flags: ACC_PUBLIC, ACC_SUPER

Constant pool:
   #1 = Class              #2             //  com/javainterviewpoint/Demo
   #2 = Utf8               com/javainterviewpoint/Demo
   #3 = Class              #4             //  java/lang/Object
   #4 = Utf8               java/lang/Object
   #5 = Utf8               
   #6 = Utf8               ()V
   #7 = Utf8               Code
   #8 = Methodref          #3.#9          //  java/lang/Object."":()V
   #9 = NameAndType        #5:#6          //  "":()V
  #10 = Utf8               LineNumberTable
  #11 = Utf8               LocalVariableTable
  #12 = Utf8               this
  #13 = Utf8               Lcom/javainterviewpoint/Demo;
  #14 = Utf8               disp
  #15 = Fieldref           #16.#18        //  java/lang/System.out:Ljava/io/PrintStream;
  #16 = Class              #17            //  java/lang/System
  #17 = Utf8               java/lang/System
  #18 = NameAndType        #19:#20        //  out:Ljava/io/PrintStream;
  #19 = Utf8               out
  #20 = Utf8               Ljava/io/PrintStream;
  #21 = String             #22            //  Welcome to JavaInterviewPoint!!!
  #22 = Utf8               Welcome to JavaInterviewPoint!!!
  #23 = Methodref          #24.#26        //  java/io/PrintStream.println:(Ljava/lang/String;)V
  #24 = Class              #25            //  java/io/PrintStream
  #25 = Utf8               java/io/PrintStream
  #26 = NameAndType        #27:#28        //  println:(Ljava/lang/String;)V
  #27 = Utf8               println
  #28 = Utf8               (Ljava/lang/String;)V
  #29 = Utf8               main
  #30 = Utf8               ([Ljava/lang/String;)V
  #31 = Methodref          #1.#9          //  com/javainterviewpoint/Demo."":()V
  #32 = Methodref          #1.#33         //  com/javainterviewpoint/Demo.disp:()V
  #33 = NameAndType        #14:#6         //  disp:()V
  #34 = Utf8               args
  #35 = Utf8               [Ljava/lang/String;
  #36 = Utf8               d1
  #37 = Utf8               SourceFile
  #38 = Utf8               Demo.java
{
  public com.javainterviewpoint.Demo();
    flags: ACC_PUBLIC

    Code:
      stack=1, locals=1, args_size=1
         0: aload_0       
         1: invokespecial #8                  // Method java/lang/Object."":()V
         4: return        
      LineNumberTable:
        line 3: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0       5     0  this   Lcom/javainterviewpoint/Demo;

  public void disp();
    flags: ACC_PUBLIC

    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #15                 // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #21                 // String Welcome to JavaInterviewPoint!!!
         5: invokevirtual #23                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return        
      LineNumberTable:
        line 7: 0
        line 8: 8
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0       9     0  this   Lcom/javainterviewpoint/Demo;

  public static void main(java.lang.String[]);
    flags: ACC_PUBLIC, ACC_STATIC

    Code:
      stack=2, locals=2, args_size=1
         0: new           #1                  // class com/javainterviewpoint/Demo
         3: dup           
         4: invokespecial #31                 // Method "":()V
         7: astore_1      
         8: aload_1       
         9: invokevirtual #32                 // Method disp:()V
        12: return        
      LineNumberTable:
        line 11: 0
        line 12: 8
        line 13: 12
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0      13     0  args   [Ljava/lang/String;
               8       5     1    d1   Lcom/javainterviewpoint/Demo;
}

you can also add different options in the Arguments block for different output. The complete list of the option which can be used along with Java Disassembler is present here.

Happy Learning !!:)

Leave a Reply

Your email address will not be published. Required fields are marked *