Javap Command gives overview of the class file . It is also called java class disassembler.
It gives the information depending upon the options used by the user .
1. Programmer NOT providing any helper option with javap command
Results in javap command shows all the package,public and protected fields and methods of the classes passed . We can also make the similar program with the help of reflection . Reflection is one of the core features of java allowing access to the Class class object .
javap command example without using options :
Compile the following class JavapExample
The output from javap JavapExample.class will be :
2. Other options Programmer has for the javap command are as follows :
-help : it will print the help messages to the user
-l : it print out all the local and line variables of the table
-public : it only prints the public members and classes
-protected : it only prints the protected and public members and classes .
-c : it print out the disassembled code
-j : flag is directly pass to the runtime
-s : outputs the internal type signature
javap command example using options :
Compile the following class JavapExample
The output from javap -public JavapExample.class will be :
Here in below code we use forName() and other reflection methods ,
But the most important question arises
What is the difference between forName() method , Class.getClass() method . Why there are two different names as both have almost same functionality .
Actually there is very minute difference , forName() loads and initializes the class while object.getClass() returns the class object loaded into the permgen space .
You can find the javap.exe in the bin directory of the jdk not in jre.
For java 1.6 the path is
jdk1.6.0_20\bin\javap.exe
So in short we use javap command to read the bytecodes .
For more info about javap : http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javap.html
Demo for Java inbuilt functionality of javap command when you type on console
After running this program the output will be look like this :
Code :
It gives the information depending upon the options used by the user .
1. Programmer NOT providing any helper option with javap command
Results in javap command shows all the package,public and protected fields and methods of the classes passed . We can also make the similar program with the help of reflection . Reflection is one of the core features of java allowing access to the Class class object .
javap command example without using options :
Compile the following class JavapExample
public class JavapExample {
String firstname ;
String lastname ;
public void printName(){
System.out.println("Full name of the candidate is " + firstname + lastname);
}
public void printNameUSAformat(){
System.out.println("Full name of the candidate in reverse is " + lastname + firstname);
}
}
The output from javap JavapExample.class will be :
Compiled from "JavapExample.java"
public class JavapExample extends java.lang.Object {
java.lang.String firstname;
java.lang.String lastname;
public JavapExample();
public void printName();
public void printNameUSAformat();
}
2. Other options Programmer has for the javap command are as follows :
-help : it will print the help messages to the user
-l : it print out all the local and line variables of the table
-public : it only prints the public members and classes
-protected : it only prints the protected and public members and classes .
-c : it print out the disassembled code
-j : flag is directly pass to the runtime
-s : outputs the internal type signature
javap command example using options :
Compile the following class JavapExample
public class JavapExample {
String firstname ;
String lastname ;
public void printName(){
System.out.println("Full name of the candidate is " + firstname + lastname);
}
public void printNameUSAformat(){
System.out.println("Full name of the candidate in reverse is " + lastname + firstname);
}
}
The output from javap -public JavapExample.class will be :
Compiled from "JavapExample.java"
public class JavapExample extends java.lang.Object {
public JavapExample();
public void printName();
public void printNameUSAformat();
}
Here in below code we use forName() and other reflection methods ,
But the most important question arises
What is the difference between forName() method , Class.getClass() method . Why there are two different names as both have almost same functionality .
Actually there is very minute difference , forName() loads and initializes the class while object.getClass() returns the class object loaded into the permgen space .
You can find the javap.exe in the bin directory of the jdk not in jre.
For java 1.6 the path is
jdk1.6.0_20\bin\javap.exe
So in short we use javap command to read the bytecodes .
For more info about javap : http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javap.html
Demo for Java inbuilt functionality of javap command when you type on console
![]() |
After running this program the output will be look like this :
Code :
import java.lang.reflect.*;
import java.awt.*;
import java.io.*;
public class Javap
{
public static void printStart(String s)
{
System.out.println("Compiled from "+s );
try
{
Class c=Class.forName(s);
int m=c.getModifiers();
printModifier(m);
System.out.print(" class "+s+" ");
}
catch(Exception e){}
}
public static void printModifier(int m)
{
if(Modifier.isPublic(m))
System.out.print("public ");
if(Modifier.isStatic(m))
System.out.print("static ");
if(Modifier.isAbstract(m))
System.out.print("abstract ");
if(Modifier.isFinal(m))
System.out.print("final ");
}
public static void printSuperClass(String s)
{
try
{
Class subclass=Class.forName(s);
Class superclass=subclass.getSuperclass();
System.out.print("extends "+superclass.getName()+" ");
}
catch(Exception e){}
}
public static void printInterface(String s)
{
try
{
Class c=Class.forName(s);
Class inter[]=c.getInterfaces();
System.out.print("implements ");
for(int i=0;i<inter.length;i++)
{
System.out.print(inter[i].getName());
System.out.print(", ");
}
System.out.println(" ");
System.out.println(" { ");
}
catch(Exception e) {}
}
public static void printField(String s)
{
try
{
Class c=Class.forName(s);
Field f[]=c.getFields();
for(int i=0;i<f.length;i++)
{
int m=f[i].getModifiers();
printModifier(m);
System.out.print(" ");
Class type=f[i].getType();
System.out.print(type.getName());
System.out.println(" "+f[i].getName());
}
}
catch(Exception e) {}
}
public static void printConstructor(String s)
{
try
{
Class c=Class.forName(s);
Constructor cs[]=c.getConstructors();
for(int i=0;i<cs.length;i++)
{
int m=cs[i].getModifiers();
printModifier(m);
System.out.print (c.getName()+"(");
Class type[]=cs[i].getParameterTypes();
for(int k=0;k<type.length;k++)
{
System.out.print(type[k].getName()+",");
}
System.out.print(")");
System.out.println(" ");
}
}
catch(Exception e){}
}
public static void printMethods (String s)
{
try
{
Class c=Class.forName(s);
Method m[]=c.getMethods();
for(int i=0;i<m.length;i++)
{
int m1=m[i].getModifiers();
printModifier(m1);
System.out.print(m[i].getReturnType().getName());
System.out.print(" "+m[i].getName());
System.out.print("(");
Class type[]=m[i].getParameterTypes();
for(int k=0;k<type.length;k++)
{
System.out.print(type[k].getName()+",");
}
System.out.print(")");
System.out.println(" ");
}
}
catch(Exception e){}
}
public static void main(String s[])
{
printStart(s[0]);
printSuperClass(s[0]);
printInterface(s[0]);
printField(s[0]);
printConstructor(s[0]);
printMethods(s[0]);
System.out.print("} ");
}
}
No comments:
Post a Comment