Using Javap and Javadoc
This blog illustrates the use of 2 tools of Java : javap and javadoc
Java has a set of Tools which can be used to display the methods defined in a Class. These tools can be used to find out which methods have been defined in the class without reading the code.
These tools can be used to make a documentation about user defined Class or default classes which are defined in the jdk.
There are two tools discussed:
1. javap - the Java Class File Disassembler
The javap command disassembles a class file. javap prints out the package, protected, and public fields and methods of the classes passed to it.
2. javadoc -
Javadoc is a tool that parses the declarations and documentation comments in a set of source files and produces a set of HTML pages describing the classes, interfaces, constructors, methods, and fields.
The following will illustrate the use of javap and javadoc for Windows.
javap
As discussed above , javap just gives the name of the methods of the class file.
Its a simple process.
-Reach the Directory where the .class file is located , of which the methods have to be found out by using cmd-prompt of windows and
-Reach the Directory where the .class file is located , of which the methods have to be found out by using cmd-prompt of windows and
type-
javap<space>NameOfClass
Following figures shows the cmd when we disassamble the StringBuffer.class file :
Observe that all the methods with there parameter's data type have been displayed in the cmd-prompt.
For more about javap , visit -> http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javap.html
JAVADOC
This is a lovely tool ! The Javadoc tool parses the declarations and documentation comments in a set of Java source files and produces a corresponding set of HTML pages describing (by default) the public and protected classes, nested classes (but not anonymous inner classes), interfaces, constructors, methods, and fields. You can use it to generate the API (Application Programming Interface) documentation or the implementation documentation for a set of source files.
If you want to learn about the methods of a class which has many confusing methods which you cannot remember , like the String class , make HTML pages describing its methods , sorts out the methods according to your need , ready for you to study and use them.
It includes very simple steps:
1. The .java file , of which the methods have to be found , is copied into a separate folder.
2. Reaching that folder using cmd-prompt and typing:
"javadoc<space>NameOfJavaFile.java"
4. cmd-prompt will generate several lines of commands by itself.
5. Opening the folder where the .java file was put shows lots of HTML pages. The file "index.html" will have all your your methods listed with description !
Following pictures show the use of javadoc on StringBuffer class which have been copied to a folder named "StringBuffer" :
The folder in which the .java file has been copied to looks like :
Clicking on the "index" file gives* :
For more on javadoc visit->http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html
So these 2 tools are very handy and very important if a particular class and its methods are to be studied.
*Note : index page might look different for different versions of jdk. Here jdk 1.6 has been used.
This information is superb.Thanx alot.I understand this topic very easily by those examples. Thanq
ReplyDelete