Java 6 in 21 Days (5th Edition): Using the JDK on Windows

The cover of Teach Yourself Java 6 in 21 Days (5th Edition) by Rogers Cadenhead

Home Buy the Book Thanks Other Books

Editing CLASSPATH

When you compile a Java program with the JDK, you type a command such as the following at a command line:

  • javac HelloWorld.java

If you compile a program successfully, you won't see any output. Try to run the program:

  • java HelloWorld

If programs compile and run OK, the Java Development Kit has been configured successfully. You don't need to make any more changes.

If you see a "class not found" error whenever you try to compile a program, you need to make one more adjustment.

If you're using Windows XP, 2000, or NT, skip to the next section.

Setting Up CLASSPATH on Windows 95, 98, and Me

There's one more line that needs to be edited, or added, to your AUTOXEC.BAT file: The CLASSPATH.

When you run Notepad and open \AUTOEXEC.BAT, you'll see a series of MS-DOS commands like those shown below.

Editing AUTOEXEC.BAT

The SET CLASSPATH= command is followed by a series of folder and filenames separated by semi-colons.

CLASSPATH is used to help the Java compiler find the Java class library and other class files that it needs. In the preceding example, the SET CLASSPATH= command highlighted in green includes three things:

  • .
  • c:\jdk1.6\lib$classfile
  • c:\jdk1.6

A CLASSPATH can contain folders (such as c:\jdk1.6 in the preceding example) and files (c:\jdk1.6\lib$classfile). It also can contain a period character ("."), which is another way to refer to the current folder in MS-DOS.

You can see what CLASSPATH has been set to by typing the following command at an MS-DOS Prompt:

  • 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 semi-colons also.

To set up the Java Development Kit correctly, the file containing the Java class library must be included in the SET CLASSPATH= command. The interpreter has the filename tools.jar. If you installed the JDK in the C:\jdk1.6 folder on your system, tools.jar should be in C:\jdk1.6\lib.

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

  • Use CD to open the folder that contains the Java interpreter (java.exe)
  • Enter the command CD ..
  • Enter the command CD lib

The lib folder should contain 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:\javajdk\lib folder, the following line should be added at the bottom of AUTOEXEC.BAT:

  • SET CLASSPATH=%CLASSPATH%;.;c:\javajdk\lib$classfile

As you might expect, the %CLASSPATH% text keeps you from removing any other CLASSPATH commands in AUTOEXEC.BAT.

After making changes to AUTOEXEC.BAT, save the file and reboot your computer. Once this is done, try to compile one of the sample programs from this website.

Setting Up ClassPath on Windows NT, 2000, or XP

On a Windows 2000, NT, or XP system, there's one more environment variable that may need to be edited, or added, in your Control Panel: The ClassPath variable.

To see the variables set up on your computer:

Right-click the My Computer icon on the desktop or the 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 opens, as shown below.

Editing Environment Variables

The System variables section contains variables that apply to all users on the computer. Look for the ClassPath item.

If you find it, select ClassPath with the mouse and click the Edit button.

If you don't find it, click the New button near the bottom of the dialog.

Regardless of which button you select, the Edit System Variable dialog opens, as shown below.

Editing System Variables

If the Variable name text field is empty, enter the text ClassPath into it.

The ClassPath variable contains a series of file and folder names separated by semi-colons. This variable is used to help Windows find your Java classes when you run or compile a program.

Click your mouse in the Variable value text field and scroll to the end of the text (if any).

To set up the Java Development Kit correctly, the file that contains the Java class library must be included in the ClassPath variable. The file has the filename tools.jar. If you installed the JDK in the C:\jdk1.6 folder on your system, Java is probably in C:\jdk1.6\lib$classfile.

If you can't remember where you installed the JDK, you can look for Java by clicking the Start button, choosing Find, and clicking Files or Folders or Start, Search. You may find several copies in different folders. If you find several copies, you should be able to find the correct one using this method:

  • Use CD to open the folder that contains the Java interpreter (java.exe)
  • Enter the command CD ..
  • Enter the command CD lib

The lib folder should contain the right copy of tools.jar.

When you know the correct folder, click your mouse in the Variable Value text field and scroll to the end of the text, then add the text ".;" followed by the folder where tools.jar was installed.

For example, if tools.jar file is in the c:\javajdk\lib folder, the following line should be added at the end of the Variable Value field: ;c:\javajdk\bin

After making changes to the ClassPath variable, save the file and reboot your computer. Once this is done, try to compile one of the sample programs from this website.