< Day Day Up > |
Configuring the Java Development KitAfter the InstallShield wizard installs the JDK, you must edit your computer's environment variables to include references to the kit. Experienced MS-DOS users can finish setting up the JDK by adjusting two variables and then rebooting the computer:
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 InterfaceThe Java Development 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.
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. 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 Settings\Rogers> 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. If you would 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 Java Development Kit: how to create a folder, how to open a folder, and how to run a program. Opening Folders in MS-DOSWhen 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. Figure A.3. Opening a folder in a command-line window.You also can use the CD command in other ways:
One of the book's suggestions is to create a folder called J24work 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:
If you haven't created that folder yet, you can accomplish the task using an MS-DOS command. Creating Folders in MS-DOSTo 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. Figure A.4. Creating a new folder in a command-line window.If you haven't already created a J24work folder, you can do it from a command line:
After J24work has been created, you can go to it at any time from a command-line by using this command: CD \J24work 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-DOSThe 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 J24work. You'll see a list of files and subfolders contained in the J24work folder, if it contains any. After you have installed the Java Development 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 JDK you have installed. 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.5. Sun sometimes tacks on a third number, but as long as it begins with 1.5 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. Correcting Configuration ErrorsWhen 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 Java Development 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):
To correct this, you must configure your system's PATH variable. Setting the PATH on Windows 98, or MeOn 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. 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:
You can see what PATH has been set to by typing the following command at a command line: PATH To set up the Java Development 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.5 in the C:\Program Files\Java\jdk1.5.0 folder on your system, java.exe is in C:\Program Files\Java\jdk1.5.0\bin. 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:
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 Files\Java\jdk1.5.0\bin is the correct folder, the following line should be added at the bottom of AUTOEXEC.BAT: PATH c:\"Program Files"\Java\jdk1.5.0\bin;%PATH% The %PATH% text keeps you from wiping out any other PATH commands in AUTOEXEC.BAT. In this command, quote 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 Java Development 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 XPOn a Windows NT, XP, 2000, or 2003 system, you configure the Path variable using the Environment Variables dialog, one of the features of the system's Control Panel. To open this dialog box:
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 Java Development 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 JDK 1.5 in the C:\Program Files\Java\jdk1.5.0 folder on your system, java.exe is in C:\Program Files\Java\jdk1.5.0\bin. 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 what one is correct, open a command-line window and do the following for each copy you have found:
When you know the correct folder, return to the Environment Variables dialog, select Path in the System variables list, and then click Edit. The Edit System Variable dialog opens with Path in the Variable name field and a list of folders in the Variable Value field (see Figure A.8). 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 Files\Java\jdk1.5.0\bin is the correct folder, the following text should be added to the end of the Path variable: c:\Program Files\Java\jdk1.5.0\bin After making the change, click OK twice: once to close the Edit System Variable dialog, and another time to close the Environment Variables dialog. Try it: Open a command-line window and type the command java -version. If it displays the right 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. |
< Day Day Up > |