• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

How to open .class file in Java

February 8, 2016 by javainterviewpoint Leave a Comment

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 !!:)

Other interesting articles which you may like …

  • JVM Architecture – Understanding JVM Internals
  • Object and Object Class in Java
  • Difference between JDK, JRE and JVM
  • Components of Java Development Kit (JDK)
  • What is a Class in Java with Example
  • How to Set Classpath for Java in Windows
  • ClassNotFoundException Vs NoClassDefFoundError
  • How HashMap works in Java
  • How to make a class Immutable in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of polymorphism in Java
  • Types of Inheritance in Java
  • Java does not supports Multiple Inheritance Diamond Problem?

Filed Under: Core Java, Java Tagged With: Java Decomplier, Java Disassembler, javap, Javap Command, open .class file

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·