Appendix A. Using the Java Development Kit

To spur development, Sun Microsystems offers the Java Development Kit (JDK), a free tool to write programs with the language.

The JDK is a set of command-line programs that are used to create, compile, and run Java programs. Every new release of Java is accompanied by a new release of the development kit: The current version is JDK version 6.

Although more sophisticated Java programming tools such as Borland JBuilder, IntelliJ IDEA, and Eclipse are available, many programmers continue to use the Java Development Kit. I’ve been using it as my primary Java programming tool for years.

This appendix covers how to download and install the Java Development Kit, set it up on your computer, and use it to create, compile, and run a simple Java program.

It also describes how to correct the most common cause of problems for a beginning Java programmer—a misconfigured Java Development Kit.

Choosing a Java Development Tool

If you’re using a Microsoft Windows or Apple Mac OS system, you probably have a Java interpreter installed that can run Java programs.

To develop Java programs, you need more than an interpreter. You also need a compiler and other tools that are used to create, run, and test programs.

The Java Development Kit includes a compiler, interpreter, debugger, file archiving program, and several other programs.

The kit is simpler than other development tools. It does not offer a graphical user interface, text editor, or other features that many programmers rely on.

To use the kit, you type commands at a text prompt. MS-DOS, Linux, and UNIX users will be familiar with this prompt, which is also called a command line.

Here’s an example of a command you might type while using the Java Development Kit:

javac RetrieveMail.java

This command tells the javac program—the Java compiler included with the kit—to read a source code file called RetrieveMail.java and create one or more class files. These files contain compiled bytecode that can be executed by a Java interpreter.

When RetrieveMail.java is compiled, one of the files will be named RetrieveMail.class. If the class file was set up to function as an application, a Java interpreter can run it.

People who are comfortable with command-line environments will be at home using the Java Development Kit. Everyone else must become accustomed to the lack of a graphical point-and-click environment as they develop programs.

If you have another Java development tool and you’re certain it is completely compatible with Java 6, you don’t need to use the Java Development Kit. Many different development tools can be used to create the tutorial programs in this book.

Caution

If you have any doubts regarding compatibility, or this book is your first experience with the Java language, you should probably use JDK 6.

Another free alternative is NetBeans, which can be downloaded from Sun as a bundle with the kit and offers a graphical user interface for programming that functions on top of the kit.

Installing the Java Development Kit

You can download the Java Development Kit from Sun’s Java website at http://java.sun.com.

The website’s Downloads section offers links to several different versions of the Java Development Kit, and it also offers the NetBeans development environment and other products related to the language. The product you should download is called the Java Software Development Kit version 6. If you can’t find it in the Downloads section of the site, look for an Early Access section that offers beta releases.

The kit is available for the following platforms:

  • Windows 98, Me, NT, 2000, XP, Server 2003, and Vista

  • Solaris SPARC and Intel

  • Linux

The kit requires a computer with a Pentium processor that is 166MHz or faster, 32MB of memory, and 400MB of free disk space.

Tip

The Macintosh version of the kit is available directly from Apple. To find out more about Apple’s kit and download the tool, visit the website http://developer.apple.com/java/download.

When you’re looking for this product, you might find that the Java Development Kit’s version number has a second number after 6, such as “JDK 6.1.” To fix bugs and address security problems, Sun periodically issues new releases of the kit and numbers them with an extra period and digit after the main version number. Choose the most current version of JDK 6 that’s offered, whether it’s numbered 6.0, 6.1, 6.2, or higher.

Caution

Take care not to download two similarly named products from Sun by mistake: the Java Runtime Environment (JRE) 6.0 or the Java Standard Edition 6.0 Source Release.

To go directly to the kit’s download page, the current address is http://jdk6.dev.java.net.

To set up the kit, you must download and run an installation program (or install it from a CD). On Sun’s website, after you choose the version of the kit that’s designed for your operating system, you can download it as a single file.

After you have downloaded the file, you’ll be ready to set up the kit.

Windows Installation

Before installing the kit, make sure that no other Java development tools are installed on your system (assuming, of course, that you don’t need any other tool at the moment). Having more than one Java programming tool installed on your computer can often cause configuration problems with the kit.

To set up the program on a Windows system, double-click the installation file or click Start, Run from the Windows taskbar to find and run the file.

The installation wizard guides you through the process of installing the software. If you accept Sun’s terms and conditions for using the kit, you’ll be asked where to install the program, as shown in Figure A.1.

Installing the JDK.

Figure A.1. Installing the JDK.

The wizard suggests a folder where the kit should be installed. In Figure A.1, the wizard is suggesting the folder C:Program FilesJavajdk1.6.0. When you install the kit, the suggested name might be different.

To choose a different folder, click the Change button, then either select or create a new folder and click OK. The wizard returns to the Custom Setup options.

Tip

Before continuing, write down the name of the folder you have chosen. You’ll need it later to configure the kit and fix any configuration problems that may occur.

You also are asked what parts of the kit to install. By default, the wizard will install all components of the JDK:

  • Development tools—The executable programs needed to create Java software

  • Demos and samples—Java programs you can run and the source code files used to create them, which you can examine to learn about the language

  • Source code—The source code for the thousands of classes that make up the Java class library

  • Public Java Runtime Environment (JRE)—A Java interpreter you can distribute with the programs you create

If you accept the default installation, you need around 350MB of free hard disk space. You can save space by omitting everything but the program files. However, the demo programs and Java runtime environment are extremely useful, so if you have the room, it’s a good idea to install them.

The source files are not needed for any of the material in this book; they are primarily of interest to experienced Java programmers.

To prevent a component from being installed, click the hard drive icon next to its name and then choose the Don’t Install This Feature Now option.

After you choose the components to install, click the Next button to continue. You may be asked whether to set up the Java Plug-in to work with the web browsers on your system.

The Java Plug-in is an interpreter that runs Java programs incorporated into web pages. These programs, which are called applets, can work with different interpreters, but most browsers do not include an interpreter that supports the current version of the Java language. Sun offers the plug-in to provide full language support to Microsoft Internet Explorer, Mozilla Firefox, Opera, and other browsers.

After you complete the configuration, the wizard installs the kit on your system.

Configuring the Java Development Kit

After the wizard installs the kit, you must edit your computer’s environment variables to include references to the kit.

Experienced MS-DOS users can finish setting up the kit by adjusting two variables and then rebooting the computer:

  • Edit the computer’s PATH variable and add a reference to the kit’s bin folder (which is C:Program FilesJavajdk1.6.0in if you installed the kit into the C:Program FilesJavajdk1.6.0 folder).

  • Edit or create a CLASSPATH variable so that it contains a reference to the current folder—a period character and semicolon (“.;” without the quotation marks)—followed by a reference to the tools.jar file in the kit’s lib folder (which is C:Program FilesJavajdk1.6.0lib ools.jar if the kit was installed into C:Program FilesJavajdk1.6.0).

For inexperienced MS-DOS users, the following section covers in detail how to set the PATH and CLASSPATH variables on a Windows system.

Users of other operating systems should follow the instructions provided by Sun on its Java Development Kit download page.

Using a Command-line Interface

The kit requires the use of a command line to compile Java programs, run them, and handle other tasks.

A command line is a way to operate a computer entirely by typing commands at your keyboard, rather than by using a mouse. Very few programs designed for Windows users require the command line today.

Note

To get to a command line in Windows, do the following:

  • On Windows 98 or Me, choose Start, Programs, and then choose MS-DOS Prompt.

  • On Windows NT or 2000, choose Start, Programs, Accessories, and then choose Command Prompt.

  • On Windows XP or Windows Server 2003, choose Start, All Programs, Accessories, and then choose Command Prompt.

When you open a command line in Windows, a new window opens in which you can type commands.

The command line in Windows uses commands adopted from MS-DOS, the Microsoft operating system that preceded Windows. MS-DOS supports the same functions as Windows—copying, moving, and deleting files and folders; running programs; scanning and repairing a hard drive; formatting a floppy disk; and so on. Figure A.2 shows a command-line window.

Using a newly opened command-line window.

Figure A.2. Using a newly opened command-line window.

In the window, a cursor blinks on the command line whenever you can type in a new command. In Figure A.2, C:Documents and SettingsRogers> is the command line.

Because MS-DOS can be used to delete files and even format your hard drive, you should learn something about the operating system before experimenting with its commands.

Note

If you’d like to learn a lot about MS-DOS, a good book is Special Edition Using MS-DOS 6.22, Third Edition, published by Que (emphasis on the words “a lot”—the book is 1,056 pages long).

However, you only need to know a few things about MS-DOS to use the kit: how to create a folder, how to open a folder, and how to run a program.

Opening Folders in MS-DOS

When you are using MS-DOS on a Windows system, you have access to all the folders you normally use in Windows. For example, if you have a Windows folder on your C: hard drive, the same folder is accessible as C:Windows from a command line.

To open a folder in MS-DOS, type the command CD, followed by the name of the folder, and press Enter. Here’s an example:

CD C:TEMP

When you enter this command, the TEMP folder on your system’s C: drive will be opened, if it exists. After you open a folder, your command line will be updated with the name of that folder, as shown in Figure A.3.

Opening a folder in a command-line window.

Figure A.3. Opening a folder in a command-line window.

You also can use the CD command in other ways:

  • Type CD to open the root folder on the current hard drive.

  • Type CD foldername to open a subfolder matching the name you’ve used in place of foldername, if that subfolder exists.

  • Type CD .. to open the folder that contains the current folder. For example, if you are in C:WindowsFonts and you use the CD .. command, C:Windows will be opened.

One of the book’s suggestions is to create a folder called J21work where you can create the tutorial programs described in the book. If you already have done this, you can switch to that folder by using the following commands:

  1. CD

  2. CD J21work

If you haven’t created that folder yet, you can accomplish the task using an MS-DOS command.

Creating Folders in MS-DOS

To create a folder from a command line, type the command MD, followed by the name of the folder, and press Enter, as in the following example:

MD C:STUFF

The STUFF folder will be created in the root folder of the system’s C: drive. To open a newly created folder, use the CD command, followed by that folder’s name, as shown in Figure A.4.

Creating a new folder in a command-line window.

Figure A.4. Creating a new folder in a command-line window.

If you haven’t already created a J21work folder, you can do it from a command line:

  1. Change to the root folder (using the CD command).

  2. Type the command MD J21work and press Enter.

After J21work has been created, you can go to it at any time from a command line by using this command:

CD J21work

The last thing you need to learn about MS-DOS to use the Java Development Kit is how to run programs.

Running Programs in MS-DOS

The simplest way to run a program at the command line is to type its name and press Enter. For example, type DIR and press Enter to see a list of files and subfolders in the current folder.

You also can run a program by typing its name followed by a space and some options that control how the program runs. These options are called arguments.

To see an example of this, change to the root folder (using CD ) and type DIR J21work. You’ll see a list of files and subfolders contained in the J21work folder, if it contains any.

After you have installed the kit, run the Java interpreter to see that it works. Type the following command at a command line:

java -version

In the preceding example, java is the name of the Java interpreter program and -version is an argument that tells the interpreter to display its version number.

You can see an example of this in Figure A.5, but your version number might be a little different depending on what version of the kit you have installed.

Running the Java interpreter in a command-line window.

Figure A.5. Running the Java interpreter in a command-line window.

If java -version works and you see a version number, it should begin with 1.6. Sun sometimes tacks on a third number, but as long as it begins with 1.6 you are using the correct version of the Java Development Kit.

If you see an incorrect version number or a Bad command or filename error after running java -version, you need to make some changes to how the Java Development Kit is configured on your system.

Caution

Sun Microsystems has a history of confusing name and version changes related to Java. Though the language is called Java 6 and the JDK is designated JDK 6.0, the internal version number of the kit is 1.6.0. This internal number shows up in the -version command as well as the choice of installation folder for the kit.

When all else fails, run the java -version command to make sure the right development tool has been installed on your system. If it begins with 1.6, you’ve got the right tool to develop programs for Java 6.

Correcting Configuration Errors

When you are writing Java programs for the first time, the most likely source for problems is not typos, syntax errors, or other programming mistakes. Most errors result from a misconfigured kit.

If you type java -version at a command line and your system can’t find the folder that contains java.exe, you will see one of the following error messages or something similar (depending on your operating system):

  • Bad command or file name

  • 'java' is not recognized as an internal or external command, operable program, or batch file

To correct this, you must configure your system’s PATH variable.

Setting the PATH on Windows 98 or Me

On a Windows 98 or Me system, you configure the PATH variable by editing the AUTOEXEC.BAT file in the root folder of your main hard drive. This file is used by MS-DOS to set environment variables and configure how some command-line programs function.

AUTOEXEC.BAT is a text file you can edit with Windows Notepad. Start Notepad by choosing Start, Programs, Accessories, Notepad from the Windows taskbar.

The Notepad text editor opens. Choose File, Open from Notepad’s menu bar, go to the root folder on your main hard drive, and then open the file AUTOEXEC.BAT.

When you open the file, you’ll see a series of MS-DOS commands, each on its own line, as shown in Figure A.6.

Editing the AUTOEXEC.BAT file with Notepad.

Figure A.6. Editing the AUTOEXEC.BAT file with Notepad.

The only commands you need to look for are any that begin with PATH.

The PATH command is followed by a space and a series of folder names separated by semicolons. It sets up the PATH variable, a list of folders that contain command-line programs you use.

PATH is used to help MS-DOS find programs when you run them at a command line. In the preceding example, the PATH command in Figure A.6 includes two folders:

  • C:PROGRA~1MSBOB

  • C:jdk1.6.0in

You can see what PATH has been set to by typing the following command at a command line:

PATH

To set up the kit correctly, the folder that contains the Java interpreter must be included in the PATH command in AUTOEXEC.BAT.

The interpreter has the filename java.exe. If you installed JDK 1.6 in the C:Program FilesJavajdk1.6.0 folder on your system, java.exe is in C:Program FilesJavajdk1.6.0in.

If you can’t remember where you installed the kit, you can look for java.exe: Choose Start, Find, Files or Folders. You might find several copies in different folders. To see which one is correct, open a command-line window and do the following for each copy you have found:

  1. Use the CD command to open a folder that contains java.exe.

  2. Run the command java -version in that folder.

When you know the correct folder, create a blank line at the bottom of the AUTOEXEC.BAT file and add the following:

PATH rightfoldername;%PATH%

For example, if c:Program FilesJavajdk1.6.0in is the correct folder, the following line should be added at the bottom of AUTOEXEC.BAT:

PATH c:"Program Files"Javajdk1.6.0in;%PATH%

The %PATH% text keeps you from wiping out any other PATH commands in AUTOEXEC.BAT. In this command, quotation marks have been added around the folder name Program Files. Some versions of Windows require this to handle folder names that contain spaces.

After making changes to AUTOEXEC.BAT, save the file and reboot your computer. When this is done, try the java -version command

If it displays the correct version of the kit, your system is probably configured correctly. You’ll find out for sure when you try to create a sample program later in this appendix.

Setting the Path on Windows NT, 2000, or XP

On a Windows NT, XP, 2000, or 2003 system, you configure the Path variable using the Environment Variables dialog box, one of the features of the system’s Control Panel.

To open this dialog box:

  1. Right-click the My Computer icon on your desktop or choose Start, Properties. The System Properties dialog box opens.

  2. Click the Advanced tab to bring it to the front.

  3. Click the Environment Variables button. The Environment Variables dialog box opens (Figure A.7).

Setting environment variables in Windows NT, XP, 2000, or 2003.

Figure A.7. Setting environment variables in Windows NT, XP, 2000, or 2003.

You can edit two kinds of environment variables: system variables, which apply to all users on your computer, and user variables, which only apply to you.

Path is a system variable that helps MS-DOS find programs when you run them at a command line. It contains a list of folders separated by semicolons.

To set up the kit correctly, the folder that contains the Java interpreter must be included in the Path. The interpreter has the filename java.exe. If you installed the kit in the C:Program FilesJavajdk1.6.0 folder on your system, java.exe is in C:Program FilesJavajdk1.6.0in.

If you can’t remember where you installed the kit, you can look for java.exe: Choose Start, Search. You might find several copies in different folders. To see which one is correct, open a command-line window and do the following for each copy you have found:

  1. Use the CD command to open a folder that contains java.exe.

  2. Run the command java -version in that folder.

When you know the correct folder, return to the Environment Variables dialog box, select Path in the System Variables list, and then click Edit. The Edit System Variable dialog box opens with Path in the Variable Name field and a list of folders in the Variable Value field (Figure A.8).

Changing your system’s Path variable.

Figure A.8. Changing your system’s Path variable.

To add a folder to the Path, click the Variable Value field and move your cursor to the end without changing anything. At the end, add a semicolon followed by the name of the folder that contains the Java interpreter

For example, if c:Program FilesJavajdk1.6.0in is the correct folder, the following text should be added to the end of the Path variable:

c:Program FilesJavajdk1.6.0in

After making the change, click OK twice: once to close the Edit System Variable dialog box and another time to close the Environment Variables dialog box.

Try it: Open a command-line window and type the command java version.

If it displays the correct version of the Java Development Kit, your system is probably configured correctly, though you won’t know for sure until you try to use the kit later in this appendix

Using a Text Editor

Unlike more sophisticated Java development tools, the Java Development Kit does not include a text editor to use when you create source files.

For an editor or word processor to work with the kit, it must be able to save text files with no formatting.

This feature has different names in different editors. Look for a format option such as one of the following when you save a document or set the properties for a document:

  • Plain text

  • ASCII text

  • DOS text

  • Text-only

If you’re using Windows, there are several editors included with the operating system.

Windows Notepad is a no-frills text editor that only works with plain-text files. It can handle only one document at a time. Choose Start, All Programs, Accessories, Notepad to run it on Windows XP or choose Start, Programs, Accessories, Notepad on other Windows systems.

Windows WordPad is a step above Notepad. It can handle more than one document at a time and can handle both plain-text and Microsoft Word formats. It also remembers the last several documents it has worked on and makes them available from the File menu. It’s also on the Accessories menu along with Notepad.

Windows users also can use Microsoft Word, but must save files as text rather than in Word’s proprietary format. (UNIX and Linux users can author programs with emacs, pico, and vi; Macintosh users have SimpleText or any of the previously mentioned UNIX tools available for Java source file creation.)

One disadvantage of using simple text editors such as Notepad or WordPad is that they do not display line numbers as you edit.

Seeing the line number helps in Java programming because many compilers indicate the line number where an error occurred. Take a look at the following error generated by the JDK compiler:

Palindrome.java:2: Class Font not found in type declaration.

The number 2 after the name of the Java source file indicates the line that triggered the compiler error. With a text editor that supports numbering, you can go directly to that line and start looking for the error.

Usually there are better ways to debug a program with a commercial Java programming package, but kit users must search for compiler-generated errors using the line number indicated by the javac tool. This is one of the best reasons to move on to an advanced Java development program after learning the language with the kit.

Tip

Another alternative is to use the kit with a programmer’s text editor that offers line numbering and other features. One of the most popular for Java is jEdit, a free editor available for Windows, Linux, and other systems at the website http://www.jedit.org.

I can personally recommend UltraEdit, an excellent programmer and web designer’s editor that sells for $39.95 at the time of this writing. To find out more and download a trial version, visit the website http://www.ultraedit.com.

Creating a Sample Program

Now that you have installed and set up the Java Development Kit, you’re ready to create a sample Java program to make sure that it works.

Java programs begin as source code—a series of statements created using a text editor and saved as a text file. You can use any program you like to create these files, as long as it can save the file as plain, unformatted text.

The kit does not include a text editor, but most other Java development tools include a built-in editor for creating source code files.

Run your editor of choice and enter the Java program in Listing A.1. Make sure all the parentheses, braces, and quotation marks in the listing are entered correctly and capitalize everything in the program exactly as shown. If your editor requires a filename before you start entering anything, call it HelloUser.java.

Example A.1. Source Code of HelloUser.java

1: public class HelloUser {
2:     public static void main(String[] arguments) {
3:         String username = System.getProperty("user.name");
4:         System.out.println("Hello " + username);
5:     }
6: }

The line numbers and colons along the left side of Listing A.1 are not part of the program—they’re included so that I can refer to specific lines by number in each program. If you’re ever unsure about the source code of a program in this book, you can compare it to a copy on the book’s official website at the following address:

http://www.java21days.com

After you finish typing in the program, save the file somewhere on your hard drive with the name HelloUser.java. Java source files must be saved with the extension .java.

Tip

If you have created a folder called J21work, save HelloUser.java and all other Java source files from this book in that folder. This makes it easier to find them while using a command-line window.

If you’re using Windows, a text editor such as Notepad might add an extra .txt file extension to the filename of any Java source files you save. For example, HelloUser.java is saved as HelloUser.java.txt. As a workaround to avoid this problem, place quotation marks around the filename when saving a source file. Figure A.9 shows this technique being used to save the source file HelloUser.java from Windows Notepad.

Saving a source file from Windows Notepad.

Figure A.9. Saving a source file from Windows Notepad.

Tip

A better solution is to permanently associate .java files with the text editor you’ll be using: In Windows, open the folder that contains HelloUser.java and double-click the file. If you have never opened a file with the .java extension, you’ll be asked what program to use when opening files of this type. Choose your preferred editor and select the option to make your choice permanent. From this point on, you can open a source file for editing by double-clicking the file.

The purpose of this project is to test the Java Development Kit—none of the Java programming concepts used in the six-line HelloUser program are described in this appendix.

You’ll learn the basics of the language .during the first several days of Week 1, “The Java Language.” If you have figured out anything about Java simply by typing in the source code of Listing A.1, it’s entirely your own fault.

Compiling and Running the Program in Windows

Now you’re ready to compile the source file with the kit’s Java compiler, a program called javac. The compiler reads a .java source file and creates one or more .class files that can be run by a Java interpreter.

Open a command-line window and then open the folder where you saved HelloUser.java.

If you saved the file in the J21work folder inside the root folder on your main hard drive, the following MS-DOS command opens the folder:

cd J21work

When you are in the correct folder, you can compile HelloUser.java by entering the following at a command prompt:

javac HelloUser.java

Figure A.10 shows the MS-DOS commands used to switch to the J21work folder and compile HelloUser.java.

Compiling a Java program in a command-line window.

Figure A.10. Compiling a Java program in a command-line window.

The kit’s compiler does not display any message if the program compiles successfully. If there are problems, the compiler enables you to know by displaying each error along with a line that triggered the error.

If the program compiled without any errors, a file called HelloUser.class is created in the same folder that contains HelloUser.java.

The class file contains the Java bytecode that will be executed by a Java interpreter. If you get any errors, go back to your original source file and make sure that you typed it exactly as it appears in Listing A.1.

After you have a class file, you can run that file using a Java interpreter. The kit’s interpreter is called java, and it also is run from the command line.

Run the HelloUser program by switching to the folder containing HelloUser.class and entering the following:

java HelloUser

You will see the text “Hello” followed by a space and your username.

Note

When running a Java class with the kit’s Java interpreter, don’t specify the .class file extension after the name of the class. If you do, you’ll see an error such as the following:

Exception in thread "main" java.lang. NoClassDefFoundError: HelloUser/class

Figure A.11 shows the successful output of the HelloUser application along with the commands used to get to that point.

Running a Java application.

Figure A.11. Running a Java application.

If you can compile the program and run it successfully, your kit is working and you are ready to start Day 1 of this book.

If you cannot get the program to compile successfully even though you have typed it exactly at it appears in the book, there may be one last problem with how the kit is configured on your system: The CLASSPATH environment variable might need to be configured.

Setting Up the CLASSPATH Variable

All the Java programs that you write rely on two kinds of class files: the classes you create and the Java class library, a set of hundreds of classes that represent the functionality of the Java language.

The kit needs to know where to find Java class files on your system. In many cases, the kit can figure this out on its own by looking in the folder where it was installed.

You also can set it up yourself by creating or modifying another environment variable: CLASSPATH.

Setting the CLASSPATH on Windows 98 or Me

If you have compiled and run the HelloUser program successfully, the kit has been configured successfully. You don’t need to make any more changes to your system.

On the other hand, if you see a Class not found error or NoClassDefFound error whenever you try to run a program, you need to make sure your CLASSPATH variable is set up correctly.

To do this, run Windows Notepad, choose File, Open and go to the root folder on your system, and then open the file AUTOEXEC.BAT. A file containing several different MS-DOS commands is opened in the editor, as shown in Figure A.12.

Editing your system’s environment variables.

Figure A.12. Editing your system’s environment variables.

Look for a line in the file that contains the text SET CLASSPATH= command followed by a series of folder and filenames separated by semicolons.

CLASSPATH is used to help the Java compiler find the class files that it needs. The SET CLASSPATH= command in Figure A.12 included two things with a semicolon between them:

  • .

  • c:jdk1.6.0lib ools.jar

A CLASSPATH can contain folders or files. It also can contain a period character ("."), which is another way to refer to the current folder in MS-DOS.

You can see your system’s CLASSPATH variable by typing the following command at a command line:

ECHO %CLASSPATH%

If your CLASSPATH includes folders or files that you know are no longer on your computer, you should remove the references to them on the SET CLASSPATH= line in AUTOEXEC.BAT. Make sure to remove any extra semicolons also.

To set up the kit correctly, the file containing the Java class library must be included in the SET CLASSPATH= command. This file has the filename tools.jar. If you installed the kit in the C:Program FilesJavajdk1.6.0 folder on your system, tools.jar is probably in the folder C:Program FilesJavajdk1.6.0lib.

If you can’t remember where you installed the kit, you can look for tools.jar by clicking Start, Find, Files or Folders from the Windows taskbar. If you find several copies, you should be able to find the correct one using this method:

  1. Use CD to open the folder that contains the Java interpreter (java.exe).

  2. Enter the command CD ....

  3. Enter the command CD lib.

The lib folder normally contains the right copy of tools.jar.

When you know the correct location, create a blank line at the bottom of the AUTOEXEC.BAT file and add the following:

SET CLASSPATH=%CLASSPATH%;.;rightlocation

For example, if tools.jar file is in the c:Program FilesJavajdk1.6.0lib folder, the following line should be added at the bottom of AUTOEXEC.BAT:

SET CLASSPATH=%CLASSPATH%;.;c:"Program Files"Javajdk1.6.0lib	ools.jar

After making changes to AUTOEXEC.BAT, save the file and reboot your computer. After this is done, try to compile and run the HelloUser sample program again. You should be able to accomplish this after the CLASSPATH variable has been set up correctly

Setting the Classpath on Windows NT, XP, 2000 or 2003

On a Windows NT, XP, 2000, or 2003 system, you also configure the Classpath variable using the Environment Variables dialog box.

To open it

  1. Right-click the My Computer icon on your desktop or Start menu and choose Properties. The System Properties dialog box opens.

  2. Click the Advanced tab to bring it to the front.

  3. Click the Environment Variables button. The Environment Variables dialog box opens (Figure A.13).

    Setting environment variables in Windows NT, XP, 2000, or 2003.

    Figure A.13. Setting environment variables in Windows NT, XP, 2000, or 2003.

If your system has a Classpath variable, it will probably be one of the system variables. Your system may not a Classpath variable set—the kit can normally find class files without the variable.

However, if your system has a Classpath, it must be set up with at least two things: a reference to the current folder (a period) and a reference to a file that contains the Java class library, tools.jar.

If you installed the kit in the C:Program FilesJavajdk1.6.0 folder, tools.jar is in the folder C:Program FilesJavajdk1.6.0lib.

If you can’t remember where you installed the kit, you can look for tools.jar by clicking Start, Search from the Windows taskbar. If you find several copies, you should be able to find the correct one using this method

  1. Use CD to open the folder that contains the Java interpreter (java.exe).

  2. Enter the command CD ...

  3. Enter the command CD lib.

The lib folder normally contains the right copy of tools.jar.

When you know the correct folder, return to the Environment Variables dialog box shown in Figure A.13.

If your system does not have a Classpath, click the New button under the System Variables list. The New System Variable dialog box opens.

If your system has a Classpath, choose it and click the Edit button. The Edit System Variable dialog box opens.

Both boxes contain the same thing: a Variable Name field and a Variable Value field.

Enter Classpath in the Variable Name field and the correct value for your Classpath in the Variable Value field.

For example, if you installed the kit in c:Program FilesJavajdk1.6.0, your Classpath should contain the following:

;C:"Program Files"Javajdk1.6.0lib	ools.jar

Figure A.14 shows how I set up Classpath for my system, which has the kit installed in C:jdk1.6.0

Setting up a Classpath in Windows XP.

Figure A.14. Setting up a Classpath in Windows XP.

After setting up your Classpath, click OK twice: once to close the Edit or New System Variable dialog box and another time to close the Environment Variables dialog box.

Unlike Windows 98 and Me users, you don’t have to reboot the system before you can try it out. Open a new command-line window and type the command java -version.

If it displays the correct version of the kit, your system might be configured correctly and require no more adjustments. Try creating the sample HelloUser program again—it should work after the CLASSPATH variable has been set up correctly

Troubleshooting Your Kit Installation

This book has a website where you can find solutions to problems, corrections, answers to reader questions, and other useful material; go to http://www.java21days.com.

If you are still having problems with the Java Development Kit, an online version of this appendix is available on the site.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset