Using Javap Tool for Disassembling classes

javap is a free tool provided along with Java Development Kit. This free tool is capable of showing the prototypes of methods and constructors in a class. In a simple way it dis assembles class files.

Unlike JavaDoc this javap neither shows the description of each and every method/constructor, class usage nor the package description. It gives the prototype of every method in the class (that belong to the class directly i.e. excluding the inherited ones). It also provides name of the super class.


javap package.subpackage.ClassName

The following is a list of run time arguments that can be passed along with the class.

Usage: javap <options> <classes>

where possible options include:

  -help  --help  -?        Print this usage message

  -version                 Version information

  -v  -verbose             Print additional information

  -l                       Print line number and local variable tables

  -public                  Show only public classes and members

  -protected               Show protected/public classes and members

  -package                 Show package/protected/public classes

                           and members (default)

  -p  -private             Show all classes and members

  -c                       Disassemble the code

  -s                       Print internal type signatures

  -sysinfo                 Show system info (path, size, date, MD5 hash)

                           of class being processed

  -constants               Show static final constants

  -classpath <path>        Specify where to find user class files

  -bootclasspath <path>    Override location of bootstrap class files

Examples

# Print the output to a file
$ javap java.lang.String >st.txt

Compiled from "String.java"
public final class java.lang.String implements java.io.Serializable, java.lang.Comparable<java.lang.String>, java.lang.CharSequence {
  public static final java.util.Comparator<java.lang.String> CASE_INSENSITIVE_ORDER;
  public java.lang.String();
  public java.lang.String(java.lang.String);
  ... (omitted for the sake of brevity)

# View the code also (will not look like Java)
$ javap -c java.lang.String

  .... (omitted for the sake of brevity)
  public static java.lang.String valueOf(boolean);
    Code:
       0: iload_0
       1: ifeq          9
       4: ldc           #137                // String true
       6: goto          11
       9: ldc           #138                // String false
      11: areturn
  ...

# Show only public members
$ javap -public java.lang.String

# Show protected/public
$ javap -protected java.lang.String

# Show package/protected/public classes/members (default)
$ javap -package java.lang.String

# Show all
$ javap -private java.lang.String

# javap a class file in a jar
$ javap -cp snmp4j.jar org.snmp4j.PDU

# Print additional info like jar file, md5sum
$ javap -sysinfo java.lang.String

Classfile jar:file:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/jre/lib/rt.jar!/java/lang/String.class
  Last modified Oct 13, 2019; size 25000 bytes
  MD5 checksum 9e77377b521cba6ab1df22407ee4cabf
  Compiled from "String.java"

ధన్యవాదాలు (Thanks)

No comments: