Skip to lesson

Read bytecode with javap

You wrote simple Java, but you want to see what the compiler really produced, for example how a string + or a loop is compiled. The .class file is unreadable in a text editor. What do you use?

javap -c: the bytecode of each method

javap is a JDK tool that shows what is inside a .class file, including its methods and, with -c, the bytecode instructions.

javap -c ClassName prints each method's signature followed by its bytecode. You run it on compiled classes, so compile first; then give a class name (javap -c Wallet) or a path to a class file (javap -c out/Wallet.class).

Example 1 · The class we will disassemble

Wallet.javaJava
public class Wallet {    private static final int DAILY_LIMIT = 5000;    private int balance;
    public Wallet(int balance) {        this.balance = balance;    }
    public boolean pay(int amount) {        if (amount > balance || amount > DAILY_LIMIT) {            return false;        }        balance = balance - amount;        log("Payment done");        return true;    }
    private void log(String message) {        System.out.println(message);    }
    public static int fee(String upiId) {        return upiId.endsWith("@bank") ? 0 : 2;    }
    public static void main(String[] args) {        Wallet w = new Wallet(1200);        System.out.println(w.pay(300));        System.out.println(w.pay(2000));        System.out.println("Fee: " + fee("riya@bank"));    }}
Output
Payment done
true
false
Fee: 0

What this code does

  • A small UPI-style wallet: a constant, a private field, a constructor, a public method, a private method and a static method. Each one shows up differently in javap.
  • pay(300) succeeds and prints the log line; pay(2000) fails because 2000 is more than the balance of 900 left after the first payment.

Example 2 · javap -c Wallet

Terminal
javac --release 17 Wallet.javajavap -c Wallet
Sample output (javac 21, --release 17; main left out)
Compiled from "Wallet.java"
public class Wallet {
  public Wallet(int);
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iload_1
       6: putfield      #7                  // Field balance:I
       9: return

  public boolean pay(int);
    Code:
       0: iload_1
       1: aload_0
       2: getfield      #7                  // Field balance:I
       5: if_icmpgt     15
       8: iload_1
       9: sipush        5000
      12: if_icmple     17
      15: iconst_0
      16: ireturn
      17: aload_0
      18: aload_0
      19: getfield      #7                  // Field balance:I
      22: iload_1
      23: isub
      24: putfield      #7                  // Field balance:I
      27: aload_0
      28: ldc           #13                 // String Payment done
      30: invokevirtual #15                 // Method log:(Ljava/lang/String;)V
      33: iconst_1
      34: ireturn

  public static int fee(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #30                 // String @bank
       3: invokevirtual #32                 // Method java/lang/String.endsWith:(Ljava/lang/String;)Z
       6: ifeq          13
       9: iconst_0
      10: goto          14
      13: iconst_2
      14: ireturn

  ...
}

What this code does

  • The constructor Wallet(int) first calls Object.<init> (every constructor calls its parent's), then putfield stores the argument into balance. <init> is the bytecode name of a constructor.
  • DAILY_LIMIT never appears as a field read: it is a compile-time constant, so javac copied 5000 straight into the code (sipush 5000). The || became two jumps, if_icmpgt and if_icmple, and log is called with invokevirtual.
  • Offsets jump by different amounts because instructions have different sizes: iload_1 is 1 byte, getfield #7 is 3 bytes.

It works on JDK classes too: javap -c java.lang.Math. Every bytecode line has the same shape. In 2: getfield #7 // Field balance:I, 2 is the offset (the byte position inside the method), getfield is the opcode (the instruction), #7 is an operand pointing into the constant pool, and the part after // is javap's comment saying what #7 means.

Without other options, javap hides private members. The private method log below is called by pay, yet it does not appear in the listing.

javap Wallet.java fails with Error: class not found: Wallet.java. javap reads class files, not source files.

javap -p and javap -v

javap -p shows all members, including private fields and methods. Combine options freely: javap -c -p Wallet prints the bytecode of private methods too.

Example 3 · javap -p and an excerpt of javap -v

Terminal
javap -p Walletjavap -v -p Wallet
Sample output (trimmed with ...)
$ javap -p Wallet
Compiled from "Wallet.java"
public class Wallet {
  private static final int DAILY_LIMIT;
  private int balance;
  public Wallet(int);
  public boolean pay(int);
  private void log(java.lang.String);
  public static int fee(java.lang.String);
  public static void main(java.lang.String[]);
}

$ javap -v -p Wallet
Classfile /.../Wallet.class
  Last modified 15-Oct-2026; size 1441 bytes
  SHA-256 checksum 50ad6b63142415c8020f82965009a992ce32dc91eb677f06eede2063cb933f7f
  Compiled from "Wallet.java"
public class Wallet
  minor version: 0
  major version: 61
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #8                          // Wallet
  super_class: #2                         // java/lang/Object
  interfaces: 0, fields: 2, methods: 5, attributes: 3
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 = Fieldref           #8.#9          // Wallet.balance:I
   #8 = Class              #10            // Wallet
   #9 = NameAndType        #11:#12        // balance:I
  #10 = Utf8               Wallet
  #11 = Utf8               balance
  #12 = Utf8               I
  #13 = String             #14            // Payment done
  #14 = Utf8               Payment done
  #15 = Methodref          #8.#16         // Wallet.log:(Ljava/lang/String;)V
  #16 = NameAndType        #17:#18        // log:(Ljava/lang/String;)V
  ...
{
  ...
  public boolean pay(int);
    descriptor: (I)Z
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=3, locals=2, args_size=2
         0: iload_1
         1: aload_0
         2: getfield      #7                  // Field balance:I
         5: if_icmpgt     15
         8: iload_1
         9: sipush        5000
        12: if_icmple     17
        15: iconst_0
        16: ireturn
        17: aload_0
        18: aload_0
        19: getfield      #7                  // Field balance:I
        22: iload_1
        23: isub
        24: putfield      #7                  // Field balance:I
        27: aload_0
        28: ldc           #13                 // String Payment done
        30: invokevirtual #15                 // Method log:(Ljava/lang/String;)V
        33: iconst_1
        34: ireturn
      LineNumberTable:
        line 10: 0
        line 11: 15
        line 13: 17
        line 14: 27
        line 15: 33
      StackMapTable: number_of_entries = 2
        frame_type = 15 /* same */
        frame_type = 1 /* same */

  ...
}

What this code does

  • -p adds the private field balance, the private constant DAILY_LIMIT and the private method log that plain javap hid.
  • -v adds the header (version 61, flags), the constant pool and per-method details. Entry #13 is the string Payment done, which ldc #13 in pay loads; #15 is the method reference Wallet.log:(Ljava/lang/String;)V.
  • StackMapTable lists type information at jump targets. The JVM's verifier uses it to check the bytecode quickly; you can ignore it when reading code.

javap -v (verbose) prints everything: the class file version, flags, the full constant pool, and for each method its descriptor, flags, stack and local sizes, bytecode and tables. It is long, so use it when you need the constant pool or the exact numbers. In -v output, stack=3, locals=2, args_size=2 says the method needs at most 3 values on its operand stack and 2 local variable slots, and receives 2 arguments. For an instance method the hidden this counts as an argument, so pay(int) has args_size=2.

Method descriptors

A method descriptor is the compact type of a method inside a class file: parameter types in brackets, then the return type. (I)V takes an int and returns void; (Ljava/lang/String;)I takes a String and returns an int.

Example 4 · Print descriptors from Java code

DescriptorDemo.javaJava
import java.lang.invoke.MethodType;import java.lang.reflect.Method;import java.util.Arrays;import java.util.Comparator;import java.util.List;
class Shop {    void addItem(int price) { }    int countItems(String category) { return 0; }    boolean canPay(long walletPaise, double billRupees) { return true; }    String[] names(char section) { return new String[0]; }    List<String> orders(int[] tableNumbers) { return List.of(); }}
public class DescriptorDemo {    public static void main(String[] args) {        Method[] methods = Shop.class.getDeclaredMethods();        Arrays.sort(methods, Comparator.comparing(Method::getName));        for (Method m : methods) {            MethodType type = MethodType.methodType(m.getReturnType(), m.getParameterTypes());            System.out.printf("%-10s %s%n", m.getName(), type.toMethodDescriptorString());        }        System.out.println("String[] as a type: " + String[].class.descriptorString());    }}
Output
addItem    (I)V
canPay     (JD)Z
countItems (Ljava/lang/String;)I
names      (C)[Ljava/lang/String;
orders     ([I)Ljava/util/List;
String[] as a type: [Ljava/lang/String;

What this code does

  • MethodType.toMethodDescriptorString() builds the same descriptor javac writes into the class file. Class.descriptorString() (Java 12) gives the descriptor of a single type.
  • canPay(long, double) is (JD)Z: J for long and Z for boolean are the two letters people mix up. orders returns List<String>, but the descriptor keeps only the erased Ljava/util/List;.

Example 5 · javap -s -p Shop

Terminal
javap -s -p Shop
Sample output
Compiled from "DescriptorDemo.java"
class Shop {
  Shop();
    descriptor: ()V

  void addItem(int);
    descriptor: (I)V

  int countItems(java.lang.String);
    descriptor: (Ljava/lang/String;)I

  boolean canPay(long, double);
    descriptor: (JD)Z

  java.lang.String[] names(char);
    descriptor: (C)[Ljava/lang/String;

  java.util.List<java.lang.String> orders(int[]);
    descriptor: ([I)Ljava/util/List;
}

What this code does

  • -s prints the descriptor under each member, so you can check your decoding of the previous example. The default constructor Shop() is ()V.
  • The Java-style line above each descriptor still shows List<String>: javap reads the generic type from the Signature attribute, while the descriptor holds only the erased type.

Primitive types are single letters. Class types are L, the class name with slashes, and ;. Arrays add [ in front: [I is int[] and [Ljava/lang/String; is String[]. There are no spaces or commas, so (JD)Z takes a long and a double and returns a boolean. Descriptors use erased types: List<String> is just Ljava/util/List;. javap -s prints the descriptor of every member.

  • B byte, C char, S short, I int, J long (not L), F float, D double, Z boolean (not B), V void
  • Ljava/lang/String; String, Ljava/util/List; any List
  • [I int[], [[D double[][], [Ljava/lang/String; String[]

javap -l: line numbers and local variables

javap -l prints two tables per method. The LineNumberTable maps bytecode offsets to source lines: line 13: 17 means the code for line 13 starts at offset 17. Stack traces use this table to show Wallet.java:13.

Example 6 · javap -l without and with -g

Terminal
javac --release 17 Wallet.javajavap -l Walletjavac -g --release 17 Wallet.javajavap -l Wallet
Sample output (trimmed with ...)
$ javac --release 17 Wallet.java
$ javap -l Wallet
...
  public boolean pay(int);
    LineNumberTable:
      line 10: 0
      line 11: 15
      line 13: 17
      line 14: 27
      line 15: 33
...

$ javac -g --release 17 Wallet.java
$ javap -l Wallet
...
  public boolean pay(int);
    LineNumberTable:
      line 10: 0
      line 11: 15
      line 13: 17
      line 14: 27
      line 15: 33
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      35     0  this   LWallet;
          0      35     1 amount   I

  public static int fee(java.lang.String);
    LineNumberTable:
      line 23: 0
    LocalVariableTable:
      Start  Length  Slot  Name   Signature
          0      15     0 upiId   Ljava/lang/String;
...

What this code does

  • Both builds have a LineNumberTable: line 13: 17 matches offset 17 in the javap -c listing, where balance = balance - amount starts.
  • Only the -g build has a LocalVariableTable. Slot 0 of pay is this and slot 1 is amount; in static fee, slot 0 is the first parameter upiId. Start and Length give the offsets where each variable exists.

The LocalVariableTable gives each local variable slot a name and type. javac writes it only when you compile with -g, because it is debugging information. Without it, debuggers and javap see slots by number only. By default javac includes line numbers and the source file name but not local variable names. That is why stack traces from normal builds still have line numbers.

Next step

Check your understanding

11 quick questions