Advanced Java Using Gui Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ADVANCED JAVA USING GUI

NOTES
TOPIC 1: JAVA TOOLS
INTRODUCTION:
Java beginners often use javac and java to compile and run Java programs. However,
JDK comes with a lot of other powerful tools such as jar, jhat, jdb, javap, javadoc, jcmd,
javah, keytool, jconsole, jps, jstatd, jstat, jinfo, jmap, jstack and many more to enable Java
developers to program better.

JAVADOC:
Documentation is an important part of program code. It not only helps others to
understand the program, but helps the author to remind how his/her own older
programs also work. Since external documentation may easily become outdated as the
program changes, Java encourages us to write documentation directly in the source
code. It requires less efforts to update documentation as and when program code
changes.
Java standardizes the syntax and semantics of writing documentation. It also provides a
tool javadoc to generate HTML files to view the documentation from a web browser in a
convenient way.
Since documentation comments (or simply doc comments) go directly in the source code,
we must hide it from Java compiler. So, documentation is written as special comments
between the character sequence /** and */ that begin and end the comment respectively.
Note that the starting delimiter must be /** (not /*), otherwise, javadoc tool will not
process the comments. Here is an example of single line comment:
/** This is a single line java doc comment. */
Comments can spread multiple lines:
/** This is an example of comments
that spans multiple lines. */
The javadoc preserves all white spaces between /** and */. However, if leading asterisks
on each line are used, white spaces (blanks and tabs) preceding the initial asterisk (*)
characters are discarded. Here is an example:
/**
* This is also an example of comments
* that spans multiple lines. */
A doc comment may be attached with only class, interface, constructor, method, or field
declarations by writing the comment immediate before them. The following is a comment
attached with a class X.
/** Represents the class of two-dimensional geometrical points. */
public class Point {}
Similarly, the following attaches a comment with a method.
public class Point {
private int x, y;
/** Returns x coordinate of the point */
public int getX()
{ return x; }
}
NB: The first sentence of each doc comment should be a summary sentence, containing
a concise but complete description of the declared entity. The javadoc tool ignores any
doc comment placed in the body of a method.
While generating the HTML files using the Javadoc commands, it parses the declarations
and documentation comments in the specified file(s) and produces a set of HTML pages
describing, by default, the public and protected classes, interfaces, constructors,
methods, and fields. We can pass either a series of Java package names or source files to
javadoc as argument(s). Here is an example of the demonstration above class Point
written:
javadoc Point.java
This command generates a set of files as shown below:

The set of .html files generated looks like below, which gives a detailed information about
the scope of the program, its constructor, its index tree and the methods used, and the
APIs used (if any).
JAVAP:
This command line tool displays information in brief about the methods, constructors
and fields present in the specified class(es). The syntax of using javap is:
javap <options> <classes>
The name of the class(es) must be a fully qualified class name (i.e., including package
name). Here is an example:
javap java.lang.Object
This displays the information of the class Object (the root class of Java class hierarchy)
in the package java.lang. Here is the sample output:

Note that javap does not show the code of the methods/constructors. It shows only the
prototypes of methods/constructors. It is useful especially to those who want to quickly
find the method signatures of a class. For example, if you do not have any IDE and want
to quickly know the methods available on java.lang.String class, use the following simple
command:
javap java.lang.String
The javap command displays its output to stdout. If you want to store the output in a
file, use the following command instead:
javap java.lang.String > out.txt
If no option is specified, javap tool prints the package, protected, and public fields and
methods of the specified classes. In fact the output of the command can be customized
using options. The set of options themselves can be viewed using –help or --help or -?
options or without using any options as follows: javap
The options are self-explanatory. For example –p option is used to show all including the
private members of the class. This tool can also be used to disassemble the byte code in a
readable format using –c option. Note a Java class file contains the so-called byte code,
which is translatable to opcodes/ mnemonics.
Considering the above class stated Point, we execute the command: javap -c Point to
simply disassemble the code in a readable format:
It is easy to see the relation between the source code and the byte code To print more
information, use the following command: javap -c -s -verbose Point
In addition to the above output, executing this command would provide the byte code
after compilation of Point.java, as shown below:

JCMD:
This tool is used to send diagnostic commands to a specified JVM. To demonstrate how to work with jcmd,
we shall first write a simple Java program as follows:
public class LoopForEver {
public static void main(String[] args) {
while(true);
}
}
The code is really simple having only an infinite loop. The loop is inserted deliberately so that it makes a
JVM up all the time and we can inspect the JVM properties in the meanwhile. Compile the program and run
it in a terminal using the following command:
java LoopForEver
This makes a JVM running indefinitely. The jcmd expects lvmid of the target JVM. A lvmid, is typically, but not necessarily, the
operating system’s process identifier for the JVM process. To find lvmid of the JVM, we can use the command without any
arguments:
jcmd

You might also like