How do I display Java bytecode?
Author: Deron Eriksson
Description: This Java tutorial describes the javap disassembler.
Tutorial created using: Windows XP || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


The "javap" disassembler tool can be used to view JavaSW bytecode in a readable format. If javap is called for a class and no other arguments are given, we receive a basic summary view of the class.

As an example, suppose we have the following Java class file and we compile it:

JavapTest.java

package test;

public class JavapTest {

	public static void main(String[] args) {
		sayIt("Hello");
		sayIt("Goodbye");
	}

	public static void sayIt(String str) {
		System.out.println(str);
	}
}

If we perform 'javap test.JavapTest' on the JavapTest.class file, we receive the following results:

Results of 'javap test.JavapTest'

Compiled from "JavapTest.java"
public class test.JavapTest extends java.lang.Object{
    public test.JavapTest();
    public static void main(java.lang.String[]);
    public static void sayIt(java.lang.String);
}

If we add a '-c' option to the command so that we perform a 'javap -c test.JavapTest', we will see the Java bytecode for the methods in the class.

Results of 'javap -c test.JavapTest'

Compiled from "JavapTest.java"
public class test.JavapTest extends java.lang.Object{
public test.JavapTest();
  Code:
   0:	aload_0
   1:	invokespecial	#8; //Method java/lang/Object."<init>":()V
   4:	return

public static void main(java.lang.String[]);
  Code:
   0:	ldc	#16; //String Hello
   2:	invokestatic	#18; //Method sayIt:(Ljava/lang/String;)V
   5:	ldc	#22; //String Goodbye
   7:	invokestatic	#18; //Method sayIt:(Ljava/lang/String;)V
   10:	return

public static void sayIt(java.lang.String);
  Code:
   0:	getstatic	#26; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:	aload_0
   4:	invokevirtual	#32; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   7:	return

}

For more information about javap, you can consult http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javap.html.