Dissemble the class

It is hardly required to check the bytecode of java class. ‘byte code’ is nothing but the set of instructions that will be given to the JVM to perform specific operations. Checking the bytecode of your program has never been part of development although there are certain libraries which allow you to edit the byte code directly.Let’s keep the use of bytecode in development aside, even if in some critical points you may dig into the bytecode.

To view the byte code let’s first create a simple java program to print Hello World! ,

public class ViewByteCode {
  private int NUM = 10;
  public static void main(String[] args) {
    System.out.println("Hello World!");

JDK provides javap command to dissemble the class file. javap command has following syntax

javap className

If no option has specified it will just print the package, protected, and public fields of methods of the classes passed to it. I have already compiled above class with javac and now let’s use javap without any options.

D:\Test>javap ViewByteCode
Compiled from "ViewByteCode.java"
public class ViewByteCode {
 public ViewByteCode();
 public static void main(java.lang.String[]);

As you can see private variable is not there and the default constructor has been added automatically. Options of dissembling the class can be found with javap, just write javap in console and press enter and you will have list of options of javap command some of them as shown below. (NOTE : it is required to set path variable to execute javap command).

Usage: javap <options> <classes>
where possible options include:
 -help --help -? Print this usage message
 -version Version information
 -v -verbose Print additional information
 -l Print line number and local variable tables
 -public Show only public classes and members
 -protected Show protected/public classes and members
 -package Show package/protected/public classes
 and members (default)
 -p -private Show all classes and members
 -c Disassemble the code
 -s Print internal type signatures
 -sysinfo Show system info (path, size, date, MD5 has
 of class being processed
 -constants Show static final constants
 -classpath <path> Specify where to find user class files
 -bootclasspath <path> Override location of bootstrap class files

As you can see -c option dissembles the code, let’s check this out,

D:\Test>javap -c ViewByteCode
Compiled from "ViewByteCode.java"
public class ViewByteCode {
  public ViewByteCode();
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":
       4: return

  public static void main(java.lang.String[]);
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/
       3: ldc           #3                  // String Hello World!
       5: invokevirtual #4                  // Method java/io/PrintStream.printl
       8: return

Here we require to understand the exact meaning of every instruction in dissembled code but still above code has some comments to make it quite meaningful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s