Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

Creating Web Applets with Java

Creating Web Applets with Java cwa07fi.htm

Previous Page TOC Next Page

— 7 —
The Java Developer's Kit

The Java Developer's Kit (JDK) is an assemblage of all the components you need to produce your own applets. The JDK is available on a variety of platforms, and you will need to obtain and understand the JDK in order to start producing applets. This chapter talks about the elements that make up the JDK and how you can use them to start building your own applets. Once you get started with the JDK, the chapter provides the first applet you can enter from scratch: HelloWorld.

The Components of the Java Developer's Kit

At the heart of developing applets is the Java Developer's Kit. The Java Developer's Kit combines all of the tools and information that you need to program your own applets into one package. The JDK contains a number of components designed to help you compile, debug, and view your applets on a variety of different platforms.

Currently, the JDK is available for the following platforms:

In the future, the JDK may be available for more UNIX platforms, and eventually various aspects of the JDK may be assembled into commercial packages. A number of companies are already developing Integrated Development Environments (IDEs) for Java with their own compilers, which are designed to replace the Sun Java compiler. However, for the beginner, the JDK represents a good starting place and provides all the tools you need to write and compile your applets.

Each of the components provided in the JDK exists as a separate program with its own function. Unfortunately, not much integration exists among the elements in the JDK, which can make using the JDK a bit confusing at first. The following sections describe the JDK's components and how they function.

Caution: Keep in mind that each version of the Java Developer's Kit is platform-specific. The individual commands you use to evoke each component may vary. There also might be subtle variations from platform to platform based on the interface model you are using (for example, Windows versus Macintosh). The JDK elements generally function like other programs in the operating system you choose, but you can also consult the JDK documentation to find out about specific differences in the JDK for each platform.


The Java compiler is javac. The javac compiler reads in the Java code you have written and converts it to the bytecode that can be run on any platform using the Java Virtual Machine. The code you write must be contained in a file called in order to be compiled properly. The compiler produces a file called filename.class, which contains the compiled bytecode. Normally, the compiler creates the .class file in the same directory as the .java file; however, you can specify a different directory.

The compiler is invoked with the following command:

     javac [ options ] ...

The compiler takes a number of options to enable you to tweak its behavior. The -classpath option enables you to specify a directory in which the standard Java classes are stored. The javac compiler uses a number of classes that are provided with the JDK, and generally these are all stored in a common directory, such as C:\jdk\java\classes. If you have stored the classes in a different directory and want to override the default installation, you can use this option to specify the path to the new directory:

-classpath path

The -d option enables you to specify a specific output directory for the compiler. When the compiler produces the .class file, it stores the file in the directory specified. The syntax for this option is as follows:

-d directory

The -g option enables you to turn on the compiler's debug tables. The Java debugging tools can then use the debug tables to help you debug your code. The syntax for this option is as follows:


The -nowarn option disables warning output from the compiler. Warnings are errors in your code that do not prevent the code from executing, but might create unpredictable behavior or errors once the code is running. If you are aware of the warnings your code is producing, you might disable the warnings to concentrate on serious bugs, but keep in mind that the warnings still apply. The -nowarn option can help cut down the clutter of text generated when you compile the code, but keep in mind that the warnings can be useful as well. The syntax for this option is as follows:


The -O option is designed to optimize your code to run a bit faster. Optimization generally causes your files to be a bit larger, which might be a factor when considering download time. The syntax for this option is as follows:


The -verbose option causes the compiler to print out a variety of information about what source code is being compiled and what class libraries are being loaded. This information can be useful for optimizing and debugging your code. The syntax for this option is as follows:



In the Java Developer's Kit, java is the Java interpreter, also known as the Java Virtual Machine. This program enables you to run Java bytecode by translating between the Java code and your operating system. If you are programming standalone applications with Java, you can use java to execute your application. You do not need to use java directly if you are programming applets, however, because applets are designed to be used in conjunction with a browser. If you do not have access to a Java-capable browser, you can use the appletviewer (which is also part of the JDK) instead of java to view applets.


The jdb is the Java debugger, and you use it to help you locate errors in your programs. It can be invoked in conjunction with an applet or browser and will provide you with output regarding errors in your program. Unfortunately, the debugger is only a command-line debugger. You can invoke it using a command such as the following:

          C:\> jdb browser.hotjava

This command launches the debugger in conjunction with the HotJava browser. The jdb debugger also can accept the commands listed in Table 7.1.


What it does


Provides help on how to use the jdb, an extremely useful command


Enables you to print out Java objects and view their contents


Dumps all of an object's instance variables


Lists all the current threads


Dumps the stack of the current thread, or specified thread

For a novice programmer, the debugger can be very hard to use. As it so happens, when you use the javac compiler to compile a program that contains errors, the compiler provides you with information regarding the bug anyway. For example, the following code contains an error:

import java.awt.Graphics;

public class HelloWorld extends java.applet.Applet {

     public void init() {



     public void paint(Graphics g) {

          g.drawString("Hello world!", 50, 25);



Suppose you don't realize that there is an error in the code and you try to compile it using the javac compiler:

C:\java\bin\javac> javac

When you invoke the compiler, it generates the following text: ';' expected.



1 error


This information is quite useful. The compiler has informed you that there is one error in the code, and it has even given you the information necessary to find the error. The :4: following the filename indicates that the error is in line 4 of your code. The compiler also points out that the error was caused because it expected a semi-colon at the end of the line. Now you can go back and change the line to read as follows:

resize(150, 25);

Your code will now compile without any problems. Of course, most commercial products have compilers that automatically take you to the error in the code by launching an editor and highlighting the error, but Java still has a way to go. As Integrated Development Environments become available, debugging options will likely increase, and debugging will become much easier.


As previously mentioned, Java has the capability to incorporate native methods from other programming languages, such as C. With javah, you can create C language header and source files from your Java code so that you can then compile those methods using C on your machine to create the native method. For beginning applet programming, this procedure is not necessary; In fact, because applets are designed to be platform-independent, native methods are not generally necessary.


The Java Class File Disassembler that enables you to examine compiled source code is javap. You can use it to print out various methods and variables in a class in order to obtain information about the construction of a program.


One of the more useful features of Java is javadoc. It is the Java API Documentation Generator, a program that enables you to generate documentation for your applets based on the comments contained within your code. This program makes it easy to generate an immediate guide on how to use your applet that is accurate and doesn't involve much extra work on your part.

Suppose your code were commented as follows:


* The text in this comment will be turned into documentation


* <pre>

*    An example of code.

* </pre>


* @version        1.0

* @author         John Doe


class Example extends Applet {



Document comments begin with /** and can contain embedded HTML tags (such as <PRE>) and special comment tags designated by the @ symbol. Chapter 8 talks more about the specifics of using comments. When you then run javadoc with your source file,


javadoc produces a formatted HTML file that contains the documentation specified in your code. You can then use that HTML file as accompanying documentation for your applet.

To understand how javadoc works, take a look at the TicTacToe sample applet provided with the JDK. First, you need to be in the same directory as the code file because javadoc reads the code and gets its information from the comments in the code. To produce the documentation for the TicTacToe applet, invoke the javadoc program with the following line:

C:\java\demos\TicTacToe> javadoc

This line should launch javadoc, which will produce output that resembles the following:

Generating packages.html

generating documentation for the class TicTacToe

Generating index

Sorting 5 items . . . done

Generating tree


That's it! Now there should be a file called TicTacToe.html in the directory; this file is the main page for the TicTacToe applet documentation (see Figure 7.1). This documentation includes a breakdown of the applet classes used in the applet and the comments found in the applet's code. Running this program can be an excellent way to learn more about the structure or usage of an applet.

Figure 7.1. The javadoc application produces HTML-based documentation from the comments in the program code.


The appletviewer is a program that enables you to run your applets without having a Java-capable Web browser handy. The appletviewer is invoked by calling a file that contains the HTML code for your applet. The appletviewer then reads the HTML code and launches your applet in its own window on your machine.

Using the applet viewer can have several advantages over using a browser. First, the appletviewer is a relatively small program that launches very quickly. Because Web browsers do a whole lot more than just show applets, they take longer to launch and might prevent you from running an applet in the background while you tweak the code.

Second, because the appletviewer does not incorporate browser-specific features, it can be a good way to check your applet for generic compatibility issues. As discussed previously, browsers restrict applets in certain ways. The appletviewer can provide you with information about how your applet runs outside of any particular browser context. You can launch the appletviewer by typing in appletviewer, followed by the name of the HTML file for the applet you want to view:

c:\java> appletviewer example.html

This example causes the appletviewer to display the example.html file, which in turn launches an sample applet. The appletviewer then opens its own window and runs the applet ( see Figure 7.2).

Figure 7.2. An applet viewed in the appletviewer.

Note: The Java Developer's Kit does not contain a Java-specific text editor. Many IDEs may contain a text editor with features geared toward writing code, but you are free to use any text editor you choose for writing code with the JDK. Keep in mind that the editor must be able to save files as plain text, but beyond that you are free to use any program you choose.

Sample Applets

In addition to these development tools, the JDK also contains a number of sample applets that are designed to showcase the abilities of Java. The sample applets are provided complete with source code and any supplementary files, so you can run them straight off your machine using the appletviewer or your Web browser. Also, because the examples include source code, you can use the code to see how specific features were implemented, which can be a very valuable learning experience.

The applets provided with the JDK 1.0 release are the following:

Where to Find the JDK

The official source for the Java Developer's Kit is Javasoft; the JDK is also on the CD-ROM accompanying this book. You can get the JDK from the CD, or you can download the JDK directly from the Web at the following URL (see Figure 7.3):

Figure 7.3. The Javasoft site contains a page with links to download the Java Developer's Kit.

Keep in mind that this site is often very busy, and it can take up to 20 minutes to download the JDK during peak usage times. You can also obtain the files from the Javasoft FTP site:        in the /pub directory

The filenames are as follows:

JDK-beta1-mac.sea.bin             Macintosh System 7.5 binary format

JDK-beta1-mac.sea.hqx             Macintosh System 7.5 HQX format

JDK-1_0-win32-x86.exe             Windows 95 and NT version

JDK-1_0-solaris2-sparc.tar.Z      Sun Solaris (SPARC) 2.3, 2.4, and 2.5

Caution: Make sure that your FTP client is set to binary when downloading these files or they will not decompress properly.

Installing the JDK

After you download the archive of the JDK, you must install it on your machine. The specifics of how you complete the installation vary from platform to platform, so the section divides the steps depending on your type of machine: UNIX, Windows 95/NT, or the Macintosh.

Installing on UNIX

First, use FTP to obtain the compressed archive of the JDK, as shown in the following FTP session:

   $ ftp

    Name ( anonymous

    331 Guest login ok, send your complete e-mail address as password.

    Password: user@machine

        << informational messages <<

    ftp> binary

    200 Type set to I.

    ftp> cd pub

        << more informational messages <<

    250 CWD command successful.

    ftp> get JDK-1_0-solaris2-sparc.tar.Z

    200 PORT command successful.

    150 Opening BINARY mode data connection for JDK-1_0-solaris2-sparc.tar.Z (4595974 bytes).

    226 Transfer complete.

    local: JDK-1_0-solaris2-sparc.tar.Z remote: JDK-1_0-solaris2-sparc.tar.Z

    4595974 bytes received in 1.4e+02 seconds (30 Kbytes/s)

    ftp> quit

After you have used FTP to obtain the file JDK-1_0-solaris2-sparc.tar.Z, decompress the archive using the following command:

     zcat JDK-1_0-solaris2-sparc.tar.Z | tar xf -

This command creates a java directory in the current directory. The java directory will have all the tools you need to begin working with Java. Before you begin though, be sure to delete the .tar file (to clean up file space) and add the java/bin directory to your path (specified in your shell .rc file).

Caution: When you uncompress the JDK archive, do not unzip the file lib/ Doing so will adversely affect the operation of Java.

Installing on Windows (NT, 95)

The Windows versions of the JDK are self-extracting archives. Once you have downloaded the binary, you should have an executable file (with a .EXE extension) that you can run to extract the archive. Run the executable in your root directory to begin extracting the JDK. The archive creates a Java directory named C:\java that contains all the necessary files. Before running any of the applets, you need to update your environment variables for the path in your autoexec.bat file.

Caution: When you uncompress the JDK archive, do not unzip the file lib/ Doing so will adversely affect the operation of Java.

Installing on the Macintosh

The Macintosh version of the JDK is available in the binary and HQX formats. When downloading, if you choose the binary format, be sure that you have MacBinary enabled in your FTP client. If you download the HQX, make sure that you have a compression utility such as StuffIt or are using an FTP client that supports the HQX format, such as Fetch. The binary (or converted HQX document) produces a self-extracting archive that contains all the elements of the JDK. Double-click on the archive to extract it to your hard drive and begin using Java.

Where to Find Documentation

Keep in mind that Java is a full-featured programming language. Although it may seem as though this chapter has listed everything there is to know about Java, it has only scratched the surface of available information. Soon Sun will be releasing a multivolume set of complete documentation for Java, but the information will be too technical and too lengthy to be useful for most people's needs. However, a variety of documents are available at Javasoft that can help you as you learn Java and answer some more advanced questions you might have.

The Java Tool Documentation

The Java Developer's Kit tools have their own documentation that can provide you with the complete set of instructions and options available for each tool. This chapter provides you with the basic commands and options you need to get started, but you might find it useful to consult the official documentation as you progress with Java programming. You can find the JDK tools documentation at

The API Documentation

The Java Application Programming Interface (API) is the specification by which all Java programs are written. Many functions that you will want to use in your applets have already been written for you by one of the Java developers, and these functions have been categorized into packages. The API packages contain a variety of classes, objects, and methods that you will need to program anything in Java. You need to be aware of what programming resources are available and how those resources are used, and this information is in the Java API. Keep in mind that the API is a fairly technical document, and you should have an understanding of the Java basics before consulting it. But once you have a solid grasp of basic Java programming, the API will prove to be an invaluable resource.

You can find the Java 1.0 API documentation at the following URL:

The documentation exists as a HTML document to make it easier to cross-reference section, and is a useful document to have handy when embarking on a large programming project.

How to Compile Java Code

Now that you have the Java Developer's Kit downloaded and installed on your machine, it's time to start working with some Java code. The first applet you are going to work with is the Hello World applet. This applet displays the words "Hello world!" on your screen and is the simplest applet you could have that actually does something. For the time being, don't worry if you don't understand what the code for this applet means. This exercise is supposed to make you comfortable with compiling Java code and using the appletviewer before you launch into writing Java applets. You'll start with the basics of Java programming in Chapter 8 and advance from there.

First, you can either set up a directory to keep your Java code in or work straight from the Java directory. Once you start to write a lot of code, you will most likely want to keep your applets separate for organization, but for now, do things your way.

The first step to creating an applet is writing the Java code. Fire up your text editor and create a file called The file is the source code for your first applet. Remember, this code must be in a plain text file, and it also must be named in order to compile correctly. Enter the following code into your file:

import java.awt.Graphics;

public class HelloWorld extends java.applet.Applet {

     public void init() {



     public void paint(Graphics g) {

          g.drawString("Hello world!", 50, 25);



In the code you just typed in, notice this line:

     public class HelloWorld extends java.applet.Applet {

This line tells the compiler that you are creating a new class called HelloWorld, which extends an existing class called java.applet.Applet. All applets are classes, which is why you use the public class statement. The name of your class is going to be HelloWorld, which also must be the name of our file. If you were to change the line to public class MyFirstApplet, then your source code file would have to be called

After you have established your applet, you need to set up your applet with the following:

     public void init() {



This code enables you to create a graphics area that is 150 by 25 pixels where you can then draw your text with the following code:

     public void paint(Graphics g) {

          g.drawString("Hello world!", 50, 25);


All this code does is use the Graphics context drawString to draw the specified text string at the given coordinates. Chapter 8 talks more about the syntax of Java, but for now, you have a simple, functioning applet that you are ready to compile.

Tip: If you receive errors during the first time you try to compile code, don't be discouraged. The compiler is very sensitive to typographic errors in code, so double-check to make sure all of the { and } are in the correct places.

The next step is to use the javac compiler to turn your source code into a compiled Java applet that you can put on a home page. Run the Java compiler with the following command:


This command should produce a file called HelloWorld.class in the same directory. This file is your compiled applet.

Note: Macintosh users should note that the JDK components are drag-and-drop within the Macintosh OS. Instead of using a command line, you can just drag your code onto the javac icon to compile it.

As with any applet, you have to have an HTML file associated with your applet in order to view the applet with the appletviewer or a Web browser. Using your text editor, create a new file in the same directory as your code called HelloWorld.html that contains the following information:


<title>Hello World Applet</title>

<applet code="HelloWorld.class" width=150 height=25>



Once you've saved this file, you are ready to view your applet in action. You can view it by opening the HTML file from your Java-capable Web browser or with the following appletviewer command:

          appletviewer HelloWorld.html

You should then see output similar to that in Figure 7.4. Keep in mind that the way an applet looks is influenced by the method you use to view it, so if you are viewing the applet with a Web browser, it might look slightly different.

Figure 7.4. The HelloWorld Applet.

That's it! You've now created your first Web applet from scratch! You might want to get some more experience compiling applets before moving on to the following chapters. Remember, the JDK comes with a variety of sample applets, including the source code, which you can use to recompile the applets and gain some practice. When you feel comfortable compiling code, move on to Chapter 8 to learn more about the Java language.


Learning how to use the tools in the JDK is the first step in applet programming. Now you have the tools to create applets at your disposal, and you know how to use them. Keep in mind that other development environments exist; some will provide a much better programming interface, and some won't. But the JDK is the official environment from Sun and is a great way to start creating applets without investing a lot of money. You've seen how to use the JDK tools, and now you've even used those tools to create the HelloWorld applet. The next chapter moves on to discuss some more details about the Java programming language.

Previous Page TOC Next Page