Table of Contents
- About the Author
- About the Technical Editor
- We Want to Hear from You!
- Reader Services
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Configuring the Software Development Kit
After the InstallShield Wizard installs SDK 1.4, you must edit your computer's environment variables to include references to the kit.
Experienced MS-DOS users can finish setting up the SDK by adjusting two variables and then rebooting the computer:
- Edit the computer's PATH variable and add a reference to the Software Development Kit's bin folder (which is C:\j2sdk1.4.0-rc\bin if you installed the kit into the C:\j2sdk1.4.0-rc folder).
- Edit or create a CLASSPATH variable , so that it contains a reference to the current folder—a period character and semi-colon (".;" without the quotation marks)— followed by a reference to the tools.jar file in the kit's lib folder. (which is C:\j2sdk1.4.0-rc\lib\tools.jar if the kit was installed into C:\j2sdk1.4.0-rc).
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 Software Development Kit download page.
Using a Command-Line Interface
The Java Software 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 where you can type commands. The command line in Windows uses commands borrowed 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. A command-line window is shown in Figure B.5.
Figure B.5 Using a newly opened command-line window.
In the window, a cursor will blink on the command line whenever you can type in a new command with your keyboard. In Figure B.5, C:\WINDOWS> 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'd like to learn a lot about MS-DOS, a good book is Special Edition Using MS-DOS 6.22, Third Edition, published in 2001 by Que (and I do mean "a lot"—the book is 1,056 pages long).
You only need to know a few things about MS-DOS to use the Software Development 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 will 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:
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 B.6.
Figure B.6 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:\Windows\Fonts and you use the CD .. command, C:\Windows will be opened.
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 have already done this, you can switch to that folder by using the following commands:
- CD \
- CD J24work
If you haven't created that folder yet, you can accomplish the task within MS-DOS.
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:
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 B.7.
Figure B.7 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:
- Change to the root folder (using the CD \ command).
- Type the command MD J24work and press Enter.
After J24work has been created, you can go to it at any time from a command-line by using this command:
The last thing you need to learn about MS-DOS to use the Software 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 J24work . You'll see a list of files and subfolders contained in the J24work folder, whether it contains any.
After you have installed the Software Development Kit, you should run the Java interpreter to see that it works. Type the following command at a command line:
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 B.8, but your version number might be a little different depending on what version of the SDK you have installed.
Figure B.8 Running the Java interpreter in a command line window.
If java -version works and you see a version number, it should begin with 1.4 because you are using SDK 1.4. Sun sometimes tacks on a third number, but as long as it begins with 1.4 you are using the correct version of the Software 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 Software Development Kit is configured on your system.
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 Software 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):
- Bad command or filename
- '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 95, 98, or Me
On a Windows 95, 98, or Me system, you configure the PATH variable by editing the AUTOEXEC.BAT file in the root folder of the 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 clicking Start, Programs, Accessories, Notepad from the Windows taskbar. The Notepad text editor will open. 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 B.9.
Figure B.9 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 B.9 includes two folders:
You can see what PATH has been set to by typing the following command at a command line:
To set up the Software 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 SDK 1.4 in the C:\j2sdk1.4.0-rc folder on your system, java.exe is in C:\jdk1.4.0-rc\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:
- Use the CD command to open a folder that contains java.exe.
- 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:
For example, if c:\j2sdk1.4.0\bin is the correct folder, the following line should be added at the bottom of AUTOEXEC.BAT:
The %PATH% text keeps you from wiping out any other PATH commands in AUTOEXEC.BAT.
After making changes to AUTOEXEC.BAT, save the file and reboot your computer. When you are done with this, try the java -version command. If it displays the correct version of the Software 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 XP
On a Windows NT, 2000, or XP 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:
- Right-click the My Computer icon on your desktop or Start menu and choose Properties. The System Properties dialog box opens.
- Click the Advanced tab to bring it to the front.
- Click the Environment Variables button. The Environment Variables dialog box opens (Figure B.10).
Figure B.10 Setting environment variables in Windows NT, 2000, or XP.
There are two kinds of environment variables you can edit: System variables, which apply to all users on your computer, and user variables that 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 Software 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 SDK 1.4 in the C:\j2sdk1.4.0-rc folder on your system, java.exe is in C:\jdk1.4.0-rc\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:
- Use the CD command to open a folder that contains java.exe.
- Run the command java -version in that folder.
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 (Figure B.11).
Figure B.11 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:\j2sdk1.4.0\bin is the correct folder, the following text should be added to the end of the Path variable:
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 software 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.