Introduction
In this article we discuss the javap command in Java and how to create your own javap command.
Javap command in Java
The javap command disassembles compiled Java files. The javap command displays information about the variables, constructors and methods present in a class file. This may be helpful when the original source code is no longer available on a system.
The output of javap commands depend on the options used. If you do not specify an option then it simply prints out the package, public fields & protected methods of the class.
Example
Developing a program that just works the same as
the javap command
The following public methods can be used for
displaying metadata of a class.
- Method[] getDeclaredMethods() thows
SecurityException
- returns an array of method objects reflecting all the methods declared by
the class or interface represented by this Class object.
- Field[] getDeclaredFields() throws
SecurityException
- returns an array of filed objects reflecting all the fields declared by the
class or interface represented by this Class object.
- Constructor[] getDeclaredConstructor()
throws SecurityException
- return an array of Constructor objects reflecting all the constructors
declared by the class represented by the Class object.
Example
import java.lang.reflect.*;
public class JavapEx
{
public static void main(String args[]) throws Exception
{
Class cls=Class.forName(args[0]);
System.out.println("Fields are...........");
Field fld[]=cls.getDeclaredFields();
for(int i=0;i<fld.length;i++)
System.out.println(fld[i]);
System.out.println("Constructors are.....");
Constructor con[]=cls.getDeclaredConstructors();
for(int i=0;i<con.length;i++)
System.out.println(con[i]);
System.out.println("Methods are.........");
Method mthd[]=cls.getDeclaredMethods();
for(int i=0;i<mthd.length;i++)
System.out.println(mthd[i]);
}
}
Output