|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
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, youll 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:
When youre working in your development directory (described in the next section), youll 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 dont 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 youre 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, youll 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 youre 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 javacthe 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 doesnt 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 AppletTo build an applet, you need to have the JDK installed. After thats done, youll follow a four-step process. Build a Java application:
This section walks you through the first two steps. The next section, Running the Applet, describes the last two steps. Writing the CodeUse any text editor to make a program source file, as shown in Listing 37.1.
Listing 37.1 HelloApplet.javaTo Write an Applet, Start by Importing Suns 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 youll 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 dont meet your requirements. Well walk through this code in detail later in this sectionfor now, just type in the code or copy it from the CD-ROM.
|
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. |