
How to set the Java classpath
by Larry Arnoldy
For some of the exercises in
Chapter 2 of the UMUC CMIS102A and CMIS140A textbook (An Introduction
to Object Oriented
Programming with Java, 3rd Edition by Thomas Wu) predefined classes
must be
used. Predefined classes are Java classes that have already been
written and compiled into a Java packages. In fact, they are also
referred to as packages. These packages can be
either another directory, a zip file, or a special Java zip file called
a jar
file. In order to use these classes with Java applications, we must
tell
Java where they are located.
The classpath is a user defined environment variable used by Java to
determine where predefined classes are located. These instructions show
how to create it and set its value.
Note: If you are using NetBeans as
your IDE, these instructions do not apply. Please refer to the instructions for NetBeans.
The following instructions are for Windows XP and will work with
command
line Java, Textpad, or jGrasp, or other similar
Java development environments..
One example of a project
that needs predefined classes is the application Ch2WebBrowser from
chapter
two of the sample source files from Wu. This needs the javabook
package. Another example is exercise 23 from
chapter 2 of the Wu textbook. It needs the galapagos package.
Step 0 - Install the predefined
classes on your PC.
These classes must first be installed
on your PC. Use
How to
Install the Predefined Wu Classes for instructions on downloading
and installing these classes to your PC. Make sure you have noted the
exact location on your PC where you installed these files. This
location will be needed in Steps 7 and 8 below.
Step 1 - Start the Control Panel
Click the Windows Start button and then Control Panel in the box which
appears.
Step 2 - Click on Performance and
Maintenance
Step 3 - Click on System
Step 4- Click on the Advanced tab
Step 5- Click on Environment
Variables
Step 6 - Create a new user environment variable
The dialog box below has an area in the
top half for user defined environment variables. The bottom half is for
maintaining and viewing the values of Windows system environment
variables. Click on the upper New to
define a new variable, namely the classpath.
Step 7 - Create classpath
In the Variable name field, enter the
word classpath. This is the
name of the new environment variable. In the Variable value field,
enter .; (a dot and a semicolon). Do not press OK yet. The classpath is a list of
file and directory locations separated by semicolons. The dot is the
symbol for the current directory. If there is no classpath variable
defined, Java will simply use the classes in the current directory and
Java's own built in classes. If a classpath variable is defined, Java
will not use the current directory unless it is included in the value
of classpath. Therefore we start with a dot. In the next step we will
add one of Wu's predefined packages.
Step 8 - Add another path to the value of classpath
One of the predefined packages from the
Wu textbook is the javabook package. It is in the file
javabookEd3.zip. Zip files can also be used in the classpath. Here is
the hard part: You must know exactly its location on your PC. On my PC
it is located at D:\Data\00-UMUC-Wu-Java\WuClasses\javabookEd3.zip
. Your location will probably be different, but the end part must be javabookEd3.zip . Enter the
full value of the location in the Variable
value field after the dot
semicolon. Be sure to use the same upper and lower case values that
your file explorer shows. Do not click on OK yet. We need to add another
value to the classpath.
Step 9 - Add one more path to the
value of classpath
After
javabookEd3.zip enter a
semicolon and then the full directory location of the directory in
which the galapagos directory is located. Do not include the name of
the galapagos directory. On my PC, the galapagos directory is located
in the directory
D:\Data\00-UMUC-Wu-Java\WuClasses.
Add this path to the end of the existing value for classpath. Do not
forget the semicolon to separate this value from the previous. Do not
include the galapagos directory.
On my PC the full classpath is the following.
| .;D:\Data\00-UMUC-Wu-Java\WuClasses\javabookEd3.zip;D:\Data\00-UMUC-Wu-Java\WuClasses |
Yours will be different if you put
javabookEd3.zip
and
galapagos in different
directories.
Step 10 - Exit the dialog boxes.
You can now see the (start of the)
classpath variable
in the upper display window. Click on OK until the last dialog box
closes. If you later wish to change the classpath, follow the same
procedure, but highlight the classpath line and then click on Edit.
You will most likely have to exit your Java editing environment, (DOS
window, jGrasp, Textpad, or whatever) in order to make the new
classpath available to Java.
Step 11 - Use the predefined classes
in a Java application
You should now be able to use class
from
the javabook or galapagos packages in your Java applications. You must
also include an import statement at the beginning of your source code.
You only need the import statement for the package that your
application needs. If you were going to use only classes from the
javabook package and none from the galapagos project, then it would
only be necessary to include the javabook line below.
// Beginning comments
import javabook.*;
import galapagos.*;
// rest of program code
|
Step 12 - Test the predefined classes
The classpath must contain the exact
location of the predefined class on your PC. The import statement must
also be correct. If you mistype either one or made any other
mistake with them, you will see errors similar to those below. In that
case check the values of the classpath and import statements. Correct
them as needed, and try again.
----jGRASP exec: javac -g
D:\Data\00-UMUC-Wu-Java\Wu-Stuff\Solutions\Ch02\Exercise2_23.java
Exercise2_23.java:11: package galapagos does not exist
import
galapagos.*;
^
Exercise2_23.java:17: cannot resolve symbol
symbol : class Turtle
location: class Exercise2_23
Turtle turtle;
^
Exercise2_23.java:18: cannot resolve symbol
symbol : class Turtle
location: class Exercise2_23
turtle = new Turtle();
^
3 errors
|
Last modified: 13 June 2004
