javap Command in Linux



The javap command, known as the Java Disassembler, is a command-line utility that is part of the Java Development Kit (JDK). It is used to disassemble one or more Java class files and can be extremely useful for developers who need to inspect the contents of these files without the source code. The javap tool provides information about the fields, methods, and constructors within a class file, and it can display them in a human-readable format.

Here's a comprehensive guide to understanding the various options available with the javap command and how to use them effectively in Linux.

Table of Contents

Here is a comprehensive guide to the options available with the javap command −

Understanding javap Command in Linux

The javap command is a powerful tool for Java developers working on Linux systems. It's a disassembler command that comes with the JDK (Java Development Kit) and is used to disassemble one or more class files. The javap command provides detailed information about the fields, methods, and constructors present within a class file, which can be invaluable for understanding the underlying structure and implementation of a Java class.

The javap command is a powerful tool used to decompile Java class files, providing information about their structure, methods, fields, and bytecode instructions. It's invaluable for understanding the inner workings of Java classes and debugging issues −

javap
Understanding javap Command

Syntax of javap Command

The basic syntax of the javap command is as follows −

javap [options] [classes]

Where [options] represents the command-line options that modify the tool's behavior, and [classes] represents the class files that you want to disassemble.

Options of the javap Command

The following table provides a list of all the options that you can be used with the javap command −

Option Description
-help or -? Displays help message with all the options and their descriptions.
-version Prints version information for the javap tool.
-v or -verbose Provides verbose output, including private properties, protected, and package-private.
-l Prints line number and local variable tables.
-public Shows only the public classes and members.
-protected Displays protected and public classes and members.
-c Disassembles the bytecode of the classes.
-package Lists package, protected, and public classes and members, which is the default behavior.
-s Prints internal type signatures.
-sysinfo Shows system information (path, size, date, MD5 hash) of the class being processed.
constants Displays final constants of the class.

Examples of javap Command in Linux

The javap command is a powerful tool for Java developers working in Linux environments. By understanding and utilizing the various options it provides, developers can gain deeper insights into the structure and behavior of Java class files, aiding in debugging and analysis tasks.

Here are some practical examples of how to use the javap command in Linux −

  • Disassemble a Single .class File
  • Disassemble Multiple .class Files
  • Disassemble a Built-in Class File
  • Display Help Information
  • Disassembling a Single Class File
  • Using Verbose Output
  • Filtering by Access Modifier
  • Disassembling Multiple Class Files
  • Viewing System Information

Disassemble a Single .class File

To disassemble and inspect the contents of a specific .class file, you can use the following command −

javap YourClass.class
Disassemble a Single .class File

This will output the compiled code from "YourClass.java" and display the class name, constructors, and methods present within the .class file.

Disassemble Multiple .class Files

If you have multiple .class files that you want to disassemble and analyze together, you can specify them all in a single javap command −

javap path/to/FirstClass.class path/to/SecondClass.class
Disassemble Multiple .class Files

This will display the class names, constructors, and methods for each of the specified .class files.

Disassemble a Built-in Class File

You can also use javap to analyze the internal structure and implementation of a built-in Java class, such as java.util.ArrayList or java.lang.String −

javap java.util.ArrayList
Disassemble a Built-in Class File in javap

The output will show the class name, inheritance hierarchy, constructors, and methods for the specified built-in Java class.

Display Help Information

For a quick reference to the javap command's options, you can display the help documentation −

javap -help
Display Help Information Using javap

Disassembling a Single Class File

To disassemble a single .class file and view its structure, you can use the following command −

javap MyClass.class
Disassembling a Single Class File Using javap

This will display the package, protected, and public fields and methods of MyClass.

Using Verbose Output

If you need detailed information, including the bytecode, you can use the -verbose option −

javap -verbose MyClass.class
Using Verbose Output in javap Command

This will include additional information such as stack size, number of locals, and arguments for methods.

Filtering by Access Modifier

To view only the public members of a class, you can use the -public option −

javap -public MyClass.class
Filtering by Access Modifier Using javap

This will list only the public fields and methods.

Disassembling Multiple Class Files

You can also disassemble multiple class files at once by listing them all in the command −

javap MyClass1.class MyClass2.class
Disassembling Multiple Class Files in javap

This will output the details for both MyClass1 and MyClass2.

Viewing System Information

To get system information about a class file, use the -sysinfo option −

javap -sysinfo MyClass.class
Viewing System Information Using javap

This will display the path, size, date, and MD5 hash of MyClass.class.

Additional Options

  • The -l flag will print out the local variable tables for the class.
  • The -s flag will print internal type signatures of the class.
  • The -sysinfo flag will display the last update date, MD5 hash, path, and size information about the class.

For more detailed examples and explanations, you can refer to the official documentation or visit online resources that provide tutorials and use cases for the javap command. Remember, practicing with real class files and experimenting with different options will help solidify your understanding of this versatile tool.

Conclusion

Whether you're debugging, conducting code analysis, or simply curious about the inner workings of Java classes, javap is an essential tool in your development toolkit.

Advertisements