Wednesday 8 April 2015

javap : disassemble class files

javap command is used to disassemble one or more class files. To get the help about javap command, type javap in command prompt.
E:\>javap
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


Syntax to use javap
javap [ options ] classes

If no options specified, javap prints out the package, protected, and public fields and methods of the classes passed to it.

Let’s say I had Employee class like below.

public class Employee {
  private String firstName;
  private String lastName;
  private String id;

  private static int totalEmplooyees;

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  static int getTotalEmplooyees() {
    return totalEmplooyees;
  }

  static void setTotalEmplooyees(int totalEmplooyees) {
    Employee.totalEmplooyees = totalEmplooyees;
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Employee [firstName=").append(firstName).append(", lastName=").append(lastName)
        .append(", id=").append(id).append("]");
    return builder.toString();
  }
}


Compile Employee.java, to get the class file. Use javap command to get the package, protected, and public fields and methods of the class.

E:\>javap Employee.class
Compiled from "Employee.java"
public class Employee {
  public Employee();
  public java.lang.String getFirstName();
  public void setFirstName(java.lang.String);
  public java.lang.String getLastName();
  public void setLastName(java.lang.String);
  public java.lang.String getId();
  public void setId(java.lang.String);
  static int getTotalEmplooyees();
  static void setTotalEmplooyees(int);
  public java.lang.String toString();
}


To get version information
Use “javap –version”.
E:\>javap -version
1.7.0_65


To get only public memebers
Use “javap” with “public” option
E:\>javap -public Employee.class
Compiled from "Employee.java"
public class Employee {
  public Employee();
  public java.lang.String getFirstName();
  public void setFirstName(java.lang.String);
  public java.lang.String getLastName();
  public void setLastName(java.lang.String);
  public java.lang.String getId();
  public void setId(java.lang.String);
  public java.lang.String toString();
}


To get protected and public classes and members
Use “javap” with “protected” option.
E:\>javap -protected Employee.class
Compiled from "Employee.java"
public class Employee {
  public Employee();
  public java.lang.String getFirstName();
  public void setFirstName(java.lang.String);
  public java.lang.String getLastName();
  public void setLastName(java.lang.String);
  public java.lang.String getId();
  public void setId(java.lang.String);
  public java.lang.String toString();
}


To get package, protected, and public classes and members
Use “javap” with “package” option.
E:\>javap -package Employee.class
Compiled from "Employee.java"
public class Employee {
  public Employee();
  public java.lang.String getFirstName();
  public void setFirstName(java.lang.String);
  public java.lang.String getLastName();
  public void setLastName(java.lang.String);
  public java.lang.String getId();
  public void setId(java.lang.String);
  static int getTotalEmplooyees();
  static void setTotalEmplooyees(int);
  public java.lang.String toString();
}


To get all members
Use “javap” with “private” option.
E:\>javap -private Employee.class
Compiled from "Employee.java"
public class Employee {
  private java.lang.String firstName;
  private java.lang.String lastName;
  private java.lang.String id;
  private static int totalEmplooyees;
  public Employee();
  public java.lang.String getFirstName();
  public void setFirstName(java.lang.String);
  public java.lang.String getLastName();
  public void setLastName(java.lang.String);
  public java.lang.String getId();
  public void setId(java.lang.String);
  static int getTotalEmplooyees();
  static void setTotalEmplooyees(int);
  public java.lang.String toString();
}


To get path, size, date, MD5 hash of the class
Use “javap” with “sysinfo” option.
E:\>javap -sysinfo Employee.class
Classfile /E:/Employee.class
  Last modified Apr 8, 2015; size 1156 bytes
  MD5 checksum 9d99d9b8d281a302e9ad0b86657e9d7f
  Compiled from "Employee.java"
public class Employee {
  public Employee();
  public java.lang.String getFirstName();
  public void setFirstName(java.lang.String);
  public java.lang.String getLastName();
  public void setLastName(java.lang.String);
  public java.lang.String getId();
  public void setId(java.lang.String);
  static int getTotalEmplooyees();
  static void setTotalEmplooyees(int);
  public java.lang.String toString();
}

Other Articles

No comments:

Post a Comment