Components of the Java 2 SDK

Team-Fly

The Java 2 SDK comes with a robust set of tools and utilities to help you quickly write and test your Java applications. This section contains a brief summary of each of these tools; more details on each of them can be found in the documentation bundle.

java

This is the launcher or virtual machine for programs written in Java. This tool reads in the pseudo-code defined in the class, translates it to machine and operating-_specific instructions, and then executes these instructions. The general format for the command is as follows:

java [options] class [arguments] java [options] –jar file.jar [arguments]

The –jar option executes a program from a jar archive. The most commonly used option is the –cp (or classpath) option, which defines the path that is used to search for classes when the program is executing. Classpaths can contain many different search directories, all separated by semicolons. Classpaths can also contain directories of class files and the location of jar and zip files that contain supporting classes for your program.

A short help list can be obtained at any time by typing java -?.

Note 

javaw is a command that works the same as the java command, with the exception that no console window appears unless there is a problem in the program’s execution.

javac

This command is used to compile Java programs from text files containing Java commands into the pseudo-code contained in class files. The general format for the command is as follows:

javac [options] [source files] [@filelist]

If you only have a few files to compile, you can list them directly in the javac command as source files. If you have a large number of files to compile, you can create a list of them in a separate file and pass this single file name to javac with a preceding @ character.

The most commonly used option is the classpath option, which overrides the environment CLASSPATH variable.

javadoc

This command reads in Java source files and produces output in the format of HTML. This output contains the comments and general information about the class, including public and protected classes, methods, and fields. The general format for the command is:

javadoc [options] [list of java packages] [list of source files] [@file containing a list of file names]

The options for this command are numerous and can be viewed in the documentation bundle; in general, they control the content and format of HTML pages.

An example of the javadoc command can be performed on the simple “Hello World” program that we tested earlier. The command can be as simple as javadoc jsape1.java. The javadoc output and an example of the main index HTML are shown in Figures D.12 and D.13.

click to expand

Figure D.12: The output of the javadoc command, showing the list of generated HTML files

Figure D.13: An example of the index-all.html page generated for the jsape1 example

jar

This command works much like the popular Windows zip command, taking multiple source files and compressing them into a single jar file. In fact, jar actually uses the zip compression format.

Note 

You may be wondering why Java uses a separate compression tool instead of using zipped files. The reason is that an applet can download a single jar file, which can contain many classes, in a single HTTP command. Generally, this is not done with zipped files. Also, having all of the classes brought over in a single HTTP command reduces the overhead dramatically, when compared to retrieving each class on its own. Lastly, using this compression tool also speeds up the execution of the applet.

The general format for the jar command is as follows:

jar [options] [manifest] destination input-files

The two most commonly used options are the c option (for creating a jar) and the u option (for updating an existing jar). An example of a typical jar command follows:

jar cmf testjar *.class

The optional manifest parameter is the place in which we hold information about the jar file (metadata). This information includes both data control and version data.

javah

The javah command generates both the C header and C source files from a Java class. You can use this command to tie together Java and C applications.

The general format of the javah command is as follows:

javah [options] class

The location of the class must be fully qualified. A common option to use is the –stubs option, which triggers the creation of the source files (without it, only the header files are created). To generate C source code stubs, you must also use the –old option to disable the default –jni option (Java Native Interface). A complete list of options can be found in the documentation bundle.

javap

The javap command is used to disassemble a class file into a listing of packages, fields, and methods. The general format for the command is:

javap [options] class

The options generally control the level of output desired, and are discussed in the documentation bundle. An example of the javap command for our “Hello World” example is shown in Figure D.14.

Figure D.14: An example of the javap command for the jsape1 example

appletviewer

The appletviewer command allows you to view and debug applets without using a browser. The format for the command is simply:

appletviewer [options] url

The URL of the command points to an HTML page, which will contain the applet tag. One window is brought up for each applet tag in the HTML page. Basically, the appletviewer command will ignore all HTML in the document, except for OBJECT, EMBED, or APPLET tags.

Options that are available for this command are limited to –debug, -encoding, and –J (for passing options to the Java Interpreter).

This command can be demonstrated by using an example from Chapter 10, “JavaBeans, ActiveX, Applets, and the Internet.” In Chapter 10, we developed an HTML page that contained two applets. The brief HTML is reproduced here for the sake of convenience, and the result from the appletviewer command is shown in Figure D.15.

<!-Test HTML page for Applet communications —> <html> <head> <title>Test Page for AWT Applets</title></head> <body> <applet name="Jsap1007" code="Jsap1007.class" width=400 height=200>  Java not supported with this browser </applet> <applet name="Jsap1008" code="Jsap1008.class" width=400 height=200> </applet> </body> </html>

Figure D.15: An example of the appletviewer command for an HTML page with two applets

extcheck

The extcheck command checks a specified jar file for conflicts to any extensions in the Java SDK. It does so by comparing the version number in the manifest file to that of the currently installed jar files.

jdb

The jdb command allows you to debug your Java programs so you can find out exactly what your programs are doing. This is usually related to finding bugs in programs, but sometimes you simply want to see if there are ways to enhance program efficiency. You can use jdb as you would a java command, along with most of the options that are associated with it. The format for the jdb command is:

jdb [options] class [arguments]

Options that can be used (in addition to the options supported by the java command) are outlined in the documentation bundle. A commonly used option is the –launch option, which immediately launches the application upon startup of the jdb command. An example session with the -launch option is shown in Figure D.16.

The jdb command can also be used without the –launch option. When the command is used in this form, you are switched from your command window prompt to the debugger prompt. From this prompt you can execute several commands,

Figure D.16: An example of the jdb command using the –launch option for the “Hello World” example program

such as the step command shown in Figure D.16. Other common commands that you can enter at the debugging prompt are as follows:

  • run [class]. Starts execution of a class.

  • help. Displays a quick list of all commands.

  • print [expression]. Prints the value of an expression.

  • set [lvalue] = [value]. Sets lvalue to value.

  • stop at class:line. Sets the breakpoint at a specific line of a source file.

  • step. Executes the current line.

  • cont. Continues execution.

  • exit. Quits the debugging session.

Overall, the Java SDK provided by Sun is a very versatile product. It provides all of the tools necessary to create large Java implementations, yet is easy enough for the novice programmer to understand. Sun also provides excellent documentation, which contains many examples, as well as a helpful set of hyperlinks for easy navigation.


Team-Fly


Java & BAPI Technology for SAP
Java & BAPI Technology for SAP
ISBN: 761523057
EAN: N/A
Year: 1998
Pages: 199

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net