∟What Is "javap" Command

This section provides a quick introduction of the 'javap' command as a JVM bytecode disassembler.

What Is "javap" command tool? "javap" is a command line tool provided in JDK (Java Development Kit). It is considered as a JVM Disassembler to convert a bytecode program (.class file) into an assembly-like code.

The "javap" command reads a JVM bytecode program, and prints out:

Let's use the same Java program, Add.java, from the last tutorial as an example to try the "javap" command.

herong> more Add.java

  class Add {
    public int add(int a, int b) {
      return a + b;
    }
  }

1. Compile Add.java into bytecode Add.class:

herong> javac Add.java

herong> ls -l Add.*
  -rw-r--r--  1 herong  staff  236 Nov  5 09:40 Add.class
  -rwxrwxr-x  1 herong  staff   72 Nov  5 09:40 Add.java

2. run the "javap" command on the .class file,

herong> javap -c -v Add.class

Classfile .../Add.class
  SHA-256 checksum da2327a3...
  Compiled from "Add.java"
class Add
  minor version: 0
  major version: 65
  flags: (0x0020) ACC_SUPER
  this_class: #7                      // Add
  super_class: #2                     // java/lang/Object
  interfaces: 0, fields: 0, methods: 2, attributes: 1
Constant pool:
   #1 = Methodref          #2.#3      // java/lang/Object."<init>":()V
   #2 = Class              #4         // java/lang/Object
   #3 = NameAndType        #5:#6      // "<init>":()V
   #4 = Utf8               java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = Class              #8         // Add
   #8 = Utf8               Add
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               add
  #12 = Utf8               (II)I
  #13 = Utf8               SourceFile
  #14 = Utf8               Add.java
{
  Add();
    descriptor: ()V
    flags: (0x0000)
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0

  public int add(int, int);
    descriptor: (II)I
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=3
         0: iload_1
         1: iload_2
         2: iadd
         3: ireturn
      LineNumberTable:
        line 3: 0
}
SourceFile: "Add.java"

3. Review the output:

If we take a closer look at the Add() method near the end of the output, we should be able to recognize those 4 JVM opcodes mentioned in the last tutorial: iload_1, iload_2, iadd and ireturn. They are generated from the return statement in the Java source code:

As Java statement in Add.java:
  return a + b;

As JVM instructions in Add.class:
  1b 1c 60 ac

As printed out by the "javap" command:
  0: iload_1
  1: iload_2
  2: iadd
  3: ireturn

See the next tutorial for a walkthrough on the entire output of the "javap -v -p" command.

Table of Contents

 About This Book

 JVM (Java Virtual Machine) Specification

 Java HotSpot VM - JVM by Oracle/Sun

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 JVM Runtime Data Areas

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 OpenJ9 by Eclipse Foundation

 JRockit JVM 28.2.7 by Oracle Corporation

►JVM Bytecode and Instruction Set

 What Is JVM Bytecode

►What Is "javap" Command

 Walkthrough of "javap -c -v" Command Output

 JVM Method Frame Architecture

 What Is Constant Pool

 JVM Instruction Set

 Constant Loading Instructions

 Local Variable Array Instructions

 Shared Memory Instructions

 Arithmetic Operation Instructions

 Bitwise Operation Instructions

 Type Conversion Instructions

 Flow Control Instructions

 Method Invocation Instructions

 Miscellaneous Instructions

 JVM Assembler/Disassembler Tools

 Archived Tutorials

 References

 Index

 Full Version in PDF/EPUB