home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


To see if CLASSPATH is set on a Windows machine, type set at the command prompt. If you find that CLASSPATH is set, you can clear it by typing set CLASSPATH=. Of course, if you set CLASSPATH in a startup file such as Autoexec.bat, you’ll want to remove that entry in order to permanently unset CLASSPATH.

On a Windows 95 machine, you can edit the CLASSPATH entry in the Autoexec.bat file. On a Windows NT machine, use the System Control panel and this step-by-step procedure:

1.  From the Start menu, choose Settings, Control Panel, as shown in Figure 37.3.
2.  In the resulting folder, locate the System Control Panel and double-click it.
3.  In the System Properties dialog box, choose the Environment tab, shown in Figure 37.4.
4.  Select the CLASSPATH variable in the upper window (marked System Variables). When you click the variable name, the variable and its current value appear in the edit fields at the bottom of the dialog box.
5.  Add the path of your library directories to the value of CLASSPATH.


FIGURE 37.3  On a Windows NT machine, set up your environment variables through the System control panel.


FIGURE 37.4  In the System Properties dialog box, select the Environment tab to change the CLASSPATH variable.

When you’re working in your development directory (described in the next section), you’ll find it useful to be able to refer to the tools by name rather than having to specify the full path. Thus, you would prefer to type javac myClass.java rather than typing C:/jdk1.2/bin/javac myClass.java.

The solution is to set the PATH variable. Include the bin directory of your JDK in the PATH so that the operating system can find the JDK tools.

Setting Up a Development Directory You can place a development directory anywhere you like on your hard drive. (Remember to set the PATH variable so you don’t have to type the full path to every JDK tool.) You might store code for the Model 1000 project in C:\Projects\Model1000\ on a Windows machine, for example, or /home/myname/projects/model1000/ under UNIX. If you’re using a development environment, such as Symantec Visual Cafè or Together/J, follow the instructions that came with your tools to set up a development directory.

As you gain experience in Java, you’ll undoubtedly develop several libraries of classes (called packages). You can add these libraries to your CLASSPATH variable, or wrap your calls to Java tools in a batch file or shell script that sets its own version of CLASSPATH. To compile all the Java programs in the current directory on a Windows machine, for example, you could type

javac -classpath .;C:\usr\local\classes\;
⇒C:\projects\model1000\java\lib\classes.zip *.java

To avoid retyping this long line each time you need to compile, put this line into a batch file. If you name the file compile.bat, for instance, you now only need to type compile to invoke the javac compiler on all Java files. (If you’re using UNIX, you can get the same effect by using a shell script.)

Testing Your Installation To test your JDK installation, go to a command prompt. (On a UNIX system, go to a shell prompt.) Change to your development directory and type javac—the name of the Java compiler.

You should get a usage message back telling you about the dozen or so options available. If you get a message complaining that the command or program doesn’t exist, check your PATH variable and make sure it includes the bin subdirectory of your JDK directory. Thus, if you placed the JDK in a directory called jdk1.2, make sure your PATH variable includes jdk1.2/bin (or jdk1.2\bin in Windows).

Building Your First Applet

To build an applet, you need to have the JDK installed. After that’s done, you’ll follow a four-step process.

Build a Java application:

1.  Enter the code into a text file.
2.  Compile all classes by using javac.
3.  Write an HTML file that loads the applet.
4.  Load the HTML file by using your Web browser.

This section walks you through the first two steps. The next section, “Running the Applet,” describes the last two steps.

Writing the Code

Use any text editor to make a program source file, as shown in Listing 37.1.


NOTE:  The listing shows line numbers for reference only. Don’t enter the line numbers if you type in this program.
Complete source code for each of these programs is included on the accompanying CD-ROM.

Listing 37.1 HelloApplet.java—To Write an Applet, Start by Importing Sun’s Classes


 import java.applet.Applet;
 import java.awt.Graphics;

 public class HelloApplet extends Applet
 {
   public void paint(Graphics theGraphics)
   {
     theGraphics.drawString(“Hello, World!”, 0, 50);
   }
 }

Java is an object-oriented language, and the engineers at Sun have written a complete applet that you can use as a starting point. As you’ll see in a moment, all you have to do is build a class that inherits from java.applet.Applet, then override any methods in Applet that don’t meet your requirements. We’ll walk through this code in detail later in this section—for now, just type in the code or copy it from the CD-ROM.


NOTE:  You must use a text editor that supports long, case-sensitive filenames.

The versions of Notepad, WordPad, and Edit that come with Windows 95 work fine. Microsoft Word 95 or higher also works. Older versions of Word for 16-bit Windows do not work.

Be sure, too, to save your file as plain text. If you save the file as a word processing document, the compiler won’t be able to process it.



NOTE:  Your Java source file must have the same name as the public class. That is, you must name this file HelloApplet.java, which corresponds to the public class HelloApplet.

Furthermore, the capitalization must also match exactly. If your class is named HelloApplet (capital H, capital A) but your source file is named helloapplet.java, the program will not compile.



Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.