Search code examples
javajavap

How can I disassemble all code with javap?


I want to look at the bytecode of a Class file with javap -c, but it doesn't seem to disassassmble all my code. Here is FooBar.java:

class FooBar {
    public static void main(String[] args) {
        if (args[0].equals("foo")) {
            System.out.println("bar");
        } else {
            notFoo();
        }
    }

    private static void notFoo() {
        System.out.println("not Foo");
    }
}

Compiled with javac FooBar.java. Then I try to disassemble it with javap -c FooBar.class which outputs:

class FooBar {
  FooBar();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: aload_0
       1: iconst_0
       2: aaload
       3: ldc           #2                  // String foo
       5: invokevirtual #3                  // Method java/lang/String.equals:(Ljava/lang/Object;)Z
       8: ifeq          22
      11: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
      14: ldc           #5                  // String bar
      16: invokevirtual #6                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      19: goto          25
      22: invokestatic  #7                  // Method notFoo:()V
      25: return
}

As you can see, this only appears to disassemble the main method and doesn't show me the disassembled notFoo() method.

How can I disassemble everything?


Solution

  • javap only shows public methods by default.

    Pass the -p argument to see everything.