How do I set my classpath to use Java?
The CLASSPATH environment variable is used by the Java virtual machine and Java compiler to locate the necessary classes required for proper compilation and operation of your Java programs. For example, if your programs use the OOJ package from the Computer Science Department, you would need to set your CLASSPATH correctly in order to compile and run your programs. The following instructions will help you set your CLASSPATH under UNIX and Windows 95 / 98 / NT / 2000 / XP. Read the FAQ and if you have any additional problems contact the help desk people in Math building room 319.
-
How do I set the classpath in Unix?
This example assumes that you are using the Korn shell (ksh), the default shell for all students in the CSCI department and want to add the OOJ class libraries to your CLASSPATH. Assume that the OOJ libraries are installed in /apps/java/OOJ. Then execute the following command from a terminal window:
export CLASSPATH=$CLASSPATH:/apps/java
The net effect of this command is to take whatever exists in the current CLASSPATH (which is $CLASSPATH) and append /apps/java to the end of it. The Java virtual machine and compiler will now look in this directory whenever you try to run or compile programs that use the OOJ package. To see what's in your CLASSPATH, type in the following:
echo $CLASSPATH
Make sure that you have a . in your CLASSPATH as well so that the current directory is also searched for classes. To make these changes permanent, add the export line described above to your .kshrc.user file.
-
How do I set the classpath in Windows 95/98?
In this environment, the CLASSPATH is defined using the set command. Assume that the OOJ libraries are in C:\classfiles\OOJ. Open up a MS-DOS prompt and execute the following command:
set CLASSPATH=.;C:\classfiles;
This will ensure that Java looks for classes in the current directory and in C:\classfiles. To make the changes permanent, add the "set line" described above to your C:\autoexec.bat file. To bring it up, enter the command "sysedit" at an MS-DOS prompt.
-
How do I set the classpath in Windows NT/2000/XP?
Assume that the OOJ libraries are installed in C:\classfiles\OOJ. Then do the following:
- Select "Start » Settings » Control Panel" and double-click on "System".
- On NT, select the "Environment" tab; on 2000 and XP, select the "Advanced" tab and click on "Environment Variables".
- Under the "User Variables" section, click on "New" if the CLASSPATH variable is not already defined (if it is already defined, double-click on it so you can edit it). In the "Variable Name" text field, type in CLASSPATH if it was not already there.
- In the "Variable Value" text field, append C:\classfiles to the end of the line. You do this by entering a semi-colon (;) and then C:\classfiles (semi-colons are used to separate different components of the CLASSPATH variable). Make sure that you have a . in your CLASSPATH as well so that the current directory is also searched for classes.
- Click on "Ok" and exit from the "System" control panel.
The changes made to the CLASSPATH are now permanently in the system.
-
How do I use classes stored in .jar files?
Jar files (or archives) are simply a collection of classes stored in a single file. Jar files can be directly added to the CLASSPATH to enable you to use the classes contained within them. For example, assume that you downloaded the OOJ.jar file into your C:\downloads directory. Then, to set the CLASSPATH under Windows 95 / 98, execute the following command from a MS-DOS prompt:
set CLASSPATH=.;C:\downloads\OOJ.jar;
Now the OOJ package will be available for use in your Java programs. If you are using UNIX or Windows NT / 2000 / XP, the idea is the same: simply append the full path pointing to the location of the OOJ.jar file to the end of your CLASSPATH.
-
Further Resources
Sun's class path help for Windows: ClassPath for Windows
Sun's class path help for Unix: ClassPath for Unix
