LinuxCommandLibrary

javap

Disassemble Java class files

TLDR

Disassemble and list one or more .class files

$ javap [path/to/file1.class path/to/file2.class ...]
copy

Disassemble and list a built-in class file
$ javap java.[package].[class]
copy

Display help
$ javap -help
copy

Display version
$ javap -version
copy

SYNOPSIS

javap [options]

can be one or more class names (e.g., MyClass) or paths to .class files (e.g., path/to/MyClass.class). When providing class names, javap uses the classpath to locate them.

PARAMETERS

-c
    Disassembles the code attribute of methods, showing bytecode instructions.

-v or -verbose
    Prints stack size, number of local variables, and arguments for methods. Also prints the constant pool.

-s
    Prints internal type signatures.

-l
    Prints line number and local variable tables.

-p or -private
    Shows all classes and members (public, protected, package, private).

-package
    Shows package/protected/public classes and members (default behavior).

-protected
    Shows protected/public classes and members.

-public
    Shows only public classes and members.

-classpath or -cp
    Specifies the path to search for class files.

-bootclasspath
    Specifies the path to search for bootstrap class files.

-sysinfo
    Shows system information (path to javap, current time, user, system properties).

-version
    Prints product version and exits.

-help or -?
    Prints a help message and exits.

DESCRIPTION

javap is a command-line utility included in the Java Development Kit (JDK) that disassembles compiled Java class files. It prints information about the fields, methods, and constructors present in a .class file, including the bytecode instructions for methods. This tool is invaluable for developers who need to understand the low-level structure of compiled Java code, debug issues related to class loading or bytecode generation, or analyze the performance characteristics of compiled methods. It can display various levels of detail, from a simple signature view to a verbose output, making it a powerful introspection tool for Java Virtual Machine (JVM) bytecode.

CAVEATS

javap is primarily for analyzing compiled .class files and does not show the original Java source code. Its output, especially the bytecode, requires knowledge of JVM instructions to interpret fully. It's a low-level tool best used for debugging or deep understanding of Java compilation rather than general code review.

USAGE EXAMPLES

  • To view the public members of MyClass.class:
    javap MyClass
  • To view all members and disassemble methods with bytecode:
    javap -private -c MyClass
  • To view constant pool and detailed information for a class not in the current directory:
    javap -v -classpath /path/to/classes com.example.MyClass

PURPOSE

javap is crucial for understanding compiler optimizations, debugging complex class loading issues, verifying bytecode generation for tools like AspectJ or code manipulators, and learning about the internals of the JVM.

HISTORY

javap has been an integral part of the Java Development Kit (JDK) since its early versions. It was designed to provide developers with a means to inspect the bytecodes generated by the javac compiler, thereby offering insights into how Java source code is translated into instructions executable by the Java Virtual Machine (JVM). Its functionality has remained largely consistent over the years, with minor enhancements and bug fixes coinciding with new JDK releases. It remains a foundational tool for JVM bytecode analysis.

SEE ALSO

javac(1), java(1), jar(1), jdb(1)

Copied to clipboard