Gamelan

 Top navigation
Navigation Links

EarthWeb sites:

 



The Java Development Kit 1.1

Before you get started learning about the Java Development Kit, it's important to make sure that you have the latest version. As of this writing, the latest version of the JDK is release 1.1. JavaSoft is expected to release a version 1.1.1 with minor bug fixes in the very near future; you can check Sun's Java Web site at http://www.javasoft.com/ to see what the latest version is. This Web site provides all the latest news and information regarding Java, including the latest release of the JDK. Keep in mind that Java is still a new technology in a state of rapid change; be sure to keep an eye on this Java Web site for the latest information.

The Java Development Kit contains a variety of tools and Java development information. Following is a list of the main components of the JDK:

  • Runtime interpreter

  • Compiler

  • Applet viewer

  • Debugger

  • Class file disassembler

  • Header and stub file generator

  • Documentation generator

  • Archiver

  • Digital signer

  • Remote Method Invocation tools

  • Sample demos and source code

  • API source code

The runtime interpreter is the core runtime module for the Java system. The compiler, applet viewer, debugger, class file disassembler, header and stub file generator, and documentation generator are the primary tools used by Java developers. The demos are interesting examples of Java applets, which all come with complete source code. And finally, if you are interested in looking under the hood of Java, the complete source code for the Java API (Application Programming Interface) classes is provided.

The Runtime Interpreter

The Java runtime interpreter (java) is used to run standalone Java executable programs in compiled, bytecode format. The runtime interpreter acts as a command-line tool for running Java programs that are either nongraphical or that manage their own window frame (applications); graphical programs requiring the display support of a Web browser (applets) are executed entirely within a browser. The syntax for using the runtime interpreter follows:

java Options ClassName Arguments

The ClassName argument specifies the name of the class you want to execute. If the class resides in a package, you must fully qualify the name. You learn about classes and packages in Chap- ter 5, "Classes, Packages, and Interfaces." For example, if you want to run a class called Roids that is located in a package called ActionGames, you execute it in the interpreter like this:

java ActionGames.Roids

When the Java interpreter executes a class, what it is really doing is executing the main() method of the class. The interpreter exits when the main() method and any threads created by it are finished executing. Don't worry if you aren't yet familiar with the main() method or Java threads--they aren't critical to understanding the runtime interpreter at this point. You'll learn about them soon enough in the coming chapters. The main() method accepts a list of arguments that can be used to control the program. The Arguments argument to the interpreter specifies the arguments passed into the main() method. For example, if you have a Java class called TextFilter that performs some kind of filtering on a text file, you would likely pass the name of the file as an argument, like this:

java TextFilter SomeFile.txt

The Options argument specifies options related to how the runtime interpreter executes the Java program. Please refer to the JDK documentation for more information about the options supported in the runtime interpreter.

The Compiler

The Java compiler (javac) is used to compile Java source code files into executable Java bytecode classes. In Java, source code files have the extension .java. The Java compiler takes files with this extension and generates executable class files with the .class extension. The compiler creates one class file for each class defined in a source file. This means that it is possible for a single Java source code file to compile into multiple executable class files. When this happens, it means that the source file contains multiple class definitions.


NOTE: Even though Java source files and classes are typically given the extensions .java and .class, it is important to note that some operating systems aren't capable of fully representing these extensions because of their length. For example, Windows 3.1 is limited to three-character extensions, so Java source files and classes must use the extensions .jav and .cla.

The Java compiler is a command-line utility that works in a manner similar to the Java runtime interpreter. The syntax for the Java compiler follows:

javac Options Filename

The Filename argument specifies the name of the source code file you want to compile. The Options argument specifies options related to how the compiler creates the executable Java classes. Please refer to the JDK documentation for more information about the options supported by the compiler.

The Applet Viewer

The applet viewer is a tool that serves as a minimal test bed for final release Java applets. You can use the applet viewer to test your programs instead of using a full-blown Web browser. You invoke the applet viewer from a command line like this:

appletviewer Options URL

The URL argument specifies a document URL containing an HTML page with an embedded Java applet. The Options argument specifies how to run the Java applet. There is only one option supported by the applet viewer: -debug. The -debug option starts the applet viewer in the Java debugger, which enables you to debug the applet. To see the applet viewer in action, check out Figure 2.1.

Figure 2.1.

The MoleculeViewer applet running in the Java applet viewer.

Figure 2.1 shows the MoleculeViewer demo applet (which comes with the JDK) running in the applet viewer. This program was launched in the applet viewer by changing to the directory containing the MoleculeViewer HTML file and executing the following statement at the command prompt:

appletviewer example1.html

example1.html is the HTML file containing the embedded Java applet. As you can see, there's nothing complicated about running Java applets using the applet viewer. The applet viewer is a useful tool for testing Java applets in a simple environment.

The Debugger

The Java debugger (jdb) is a command-line utility that enables you to debug Java applications. The Java debugger uses the Java Debugger API to provide debugging support within the Java runtime interpreter. The syntax for using the Java debugger follows:

jdb Options

The Options argument is used to specify different settings within a debugging session. For more information about the options supported in the debugger, see Chapter 28, "Java Debugging."

The Class File Disassembler

The Java class file disassembler (javap) is used to disassemble executable Java class files. Its default output consists of the public data and methods for a class. The class file disassembler is useful in cases where you don't have the source code for a class, but you want to know a little more about how it is implemented. The syntax for the disassembler follows:

javap Options ClassNames

The ClassNames argument specifies the names of one or more classes to be disassembled. The Options argument specifies how the classes are to be disassembled. Refer to the JDK documentation for more information about the options supported in the class file disassembler.

The Header and Stub File Generator

The Java header and stub file generator (javah) is used to generate C header and source files for implementing Java methods in C. The files generated can be used to access member variables of an object from C code. The header and stub file generator accomplishes this by generating a C structure whose layout matches that of the corresponding Java class. The syntax for using the header and stub file generator follows:

javah Options ClassName

The ClassName argument is the name of the class from which to generate C source files. The Options argument specifies how the source files are to be generated. Refer to the JDK documentation for more information about the options supported in the header and stub file generator.

The Documentation Generator

The Java documentation generator (javadoc) is a useful tool for generating API documentation directly from Java source code. The documentation generator parses through Java source files and generates HTML pages based on the declarations and comments. The syntax for using the documentation generator follows:

javadoc Options FileName

The FileName argument specifies either a package or a Java source code file. In the case of a package, the documentation generator creates documentation for all the classes contained in the package. The Options argument enables you to change the default behavior of javadoc.

Because the Java documentation generator is covered in detail in Chapter 29, "Documenting Your Code," you'll have to settle for this brief introduction for now. Or you could jump ahead to Chapter 29 to learn more now.

The Archiver

The Java archiver (jar) is a tool used to combine and compress multiple files (usually applets or applications) into a single archive file, which is also commonly referred to as a JAR file. Combining the components of an applet or application into a single archive allows them to be downloaded by a browser in a single HTTP transaction instead of requiring a new connection for each individual file. This approach, coupled with the data compression provided by the archiver, dramatically improves download times. Additionally, the archiver can be used with the digital signer (described in the following section) to sign applets and applications so that they can be authenticated at runtime. The syntax for using the archiver follows:


jar Options ManifestFileName
OuputFileName InputFileNames

The ManifestFileName argument specifies a manifest file used to describe the contents of the archive being created. The OutputFileName argument specifies the name of the archive to be created. The InputFileNames argument specifies the files to be added to the archive. The Options argument enables you to change the default behavior of jar. You learn all about manifest files, code signing, and the ins and outs of the jar tool in Part VIII of this book, "Java Archives and JavaBeans."

The Digital Signer

The Java digital signer (javakey), also known as the Java security tool, is an interesting tool that generates digital signatures for archive files. Signatures are used to verify that a file came from a specified entity, or signer. To generate a signature for a particular file, the signer must first be associated with a public/private key pair and must also have one or more certificates authenticating the signer's public key. The digital signer is responsible for managing this database of entities, along with their keys and certificates. The syntax for using the digital signer follows:

javakey Options

The Options argument is used to control the operation of javakey. You learn all about digital code signing, public and private keys, and the specifics of how to use the javakey tool in Chapter 37, "Code Signing and JAR Security."

The Remote Method Invocation Tools

The JDK includes three different tools for working with and managing remote method invocation (RMI). These tools consist of an RMI stub compiler, a remote object registry tool, and a serial version tool. For more information about the RMI tools, please refer to Chapter 17, "The RMI Package."

Examples and Source Code

The JDK comes with a variety of interesting Java examples, all of which include complete source code. Following is a list of the Java example applets that come with the JDK:

  • Animator
  • ArcTest
  • BarChart
  • Blink
  • BouncingHeads
  • CardTest
  • DitherTest
  • DrawTest
  • Fractal
  • GraphicsTest
  • GraphLayout
  • ImageMap
  • ImageTest
  • JumpingBox
  • MoleculeViewer
  • NervousText
  • ScrollingImages
  • SimpleGraph
  • SpreadSheet
  • TicTacToe
  • TumblingDuke
  • UnderConstruction
  • WireFrame

Rather than go through the tedium of describing each of these examples, I'll leave most of them for you to explore and try out on your own. However, it's worth checking out a few of them here and discuss how they impact the Web.

The first demo applet we're concerned about is the BarChart applet, shown in Figure 2.2.

Figure 2.2.

The BarChart Java applet.

The BarChart applet is a good example of how Java can be used to show statistical information on the Web graphically. The data represented by the bar graph could be linked to a live data source, such as a group of stock quotes. Then you could actually generate a live, to-the-minute, dynamically changing stock portfolio.

The GraphicsTest applet is a good example of how to use Java graphics. Java includes an extensive set of graphics features, including support for drawing primitive shapes as well as more elaborate drawing routines. Figure 2.3 shows what the GraphicsTest applet looks like.

Figure 2.3.

The GraphicsTest Java applet.

Keeping the focus on graphics, the SimpleGraph applet shows how Java can be used to plot a two-dimensional graph. There are plenty of scientific and educational applications for plotting. Using Java, data presented in a Web page can come to life with graphical plots. SimpleGraph is shown in Figure 2.4.

Figure 2.4.

The SimpleGraph Java applet.

On the business front, there's nothing like a good spreadsheet. The SpreadSheet Java applet shows how to implement a simple spreadsheet in Java. I don't think I even need to say how many applications there are for interactive spreadsheets on the Web. Check out the SpreadSheet applet in Figure 2.5.

Figure 2.5.

The SpreadSheet Java applet.

Once you've gotten a headache playing with the SpreadSheet applet, it's time to blow off a little steam with a game. The applet in Figure 2.6 demonstrates a simple Java version of TicTacToe. This demo opens a new window of opportunity for having fun on the Web. Games are an interesting application for Java, so keep your eyes peeled for new and interesting ways to have fun on the Web with Java games.

Figure 2.6.

The TicTacToe Java applet.

The last applet mentioned in this chapter is the UnderConstruction applet, which is a neat little applet that can be used to jazz up unfinished Web pages. This applet shows an animation of the Java mascot, Duke, with a jackhammer. Because the applet also has sound, it's a true multimedia experience! Although this applet is strictly for fun, it nevertheless provides a cool alternative to the usual "under construction" messages that are often used in unfinished Web pages. The UnderConstruction applet is shown in Figure 2.7.

Figure 2.7.

The UnderConstruction Java applet.

Although running these example applets is neat, the real thing to keep in mind is that they all come with complete source code. This means that you can rip them apart and figure out how they work, and then use similar techniques in your own Java programs. The most powerful way to learn is by example, and the sample applets that come with the JDK are great examples of robust Java applets.

API Source Code

The final component of the Java Development Kit is the source code for the Java API. That's right--the JDK comes with the complete source code for all the classes that make up the Java API. Sun isn't concerned with keeping the internals of Java top secret. They followed the lead of the UNIX world and decided to make Java as available and readily understood as possible. Besides, the real value of Java is not the specific code that makes it work, it's the idea behind it.

The API source code is automatically installed to your hard drive when you decompress the JDK, but it may remain in compressed form, depending on your particular platform. It is sometimes very useful to be able to look under the hood to see how something works. The API source code comes compressed in a file called src.zip, located in the java directory created on your hard drive during installation of the JDK. All the classes that make up the Java API are included in this file.

Content for this section used with permission from:
JAVATM 1.1 UNLEASHED, Third Edition
(Imprint:SAMS.Net, Publisher: Macmillan Computer Publishing)
Author: Bankston and Seifert
ISBN: 1-57521-298-6

Give them a visit

Give them a visit



HomeAbout UsSearchSubscribeContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1998 EarthWeb, Inc.. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited.

Gamelon File I/O Library

Gamelon® is a registered trademark owned by Menai Corporation and Gamelan is used under license from Menai Corporation by EarthWeb, Inc. Menai Corporation is the maker of Gamelon® cross-platform software for the Internet. The adjacent logo is a trademark of Menai Corporation.