Previous Section  < Day Day Up >  Next Section

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 Java Development 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 Java Development 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.

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.5.0\lib\tools.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 Java Development 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 Files\Java\jdk1.5.0 folder on your system, tools.jar is probably in the folder C:\Program Files\Java\jdk1.5.0\lib.

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 Files\Java\jdk1.5.0\lib folder, the following line should be added at the bottom of AUTOEXEC.BAT:


SET CLASSPATH=%CLASSPATH%;.;c:\"Program Files"\Java\jdk1.5.0\lib\tools.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.

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 (see Figure A.13).

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—the Java Development 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 Files\Java\jdk1.5.0 folder, tools.jar is in the folder C:\Program Files\Java\jdk1.5.0\lib.

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 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 Java Development Kit in c:\Program Files\Java\jdk1.5.0, your Classpath should contain the following:


.;C:\"Program Files"\Java\jdk1.5.0\lib\tools.jar


Figure A.14 shows how I set up Classpath for my system, which has the Java Development Kit installed in C:\jdk1.5.0.

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.

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 right version of the Java Development 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: http://www.java24hours.com.

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

    Previous Section  < Day Day Up >  Next Section