Classpath Tutorial
Purpose
In Computer Science 235, it is required that students learn to compile and run their java programs from the command-line for two reasons: (1) To gain valuable command-line experience and (2) to aid in passoffs and obtaining help from the TAs. This being the case, we'll look at an example that will guide us through the process of compiling and running the first project.
Packages
A Java package can be thought of as being a collection of classes that all comprise a single working program. Just as we might import the java.io package or the java.lang package, so could we import our own user-defined packages later on in other programs, making our code reusable. In order to "package" java files, we need to include the following line at the beginning of all the java files that will belong in the package:
package generalname.specificname;
generalname and
specificname are replaced with the the
names that the user decides best describe the package. In this class,
the generalname will always be cs235
with the 'c' and the 's' both lower-case. The specific name will
depend upon the project. The first project will have the following
declaration: (again, remember all lower-case):
package cs235.javaproject;
Being that we have a package named like this, it is imperative that
we know where to place our programs that we will write. We must
first create a directory (or folder) named cs235
and inside that directory, another called javaproject.
The package names and the directories must cooincide exactly, including
case. So, for example, if we had made these two directories on the root
of the F: drive, the full path would be
F:\cs235\javaproject\
and all the files for
the project would be placed inside the javaproject directory. It is
recommended that the cs235 directory be placed on the root of a drive,
so that the classpath (discussed next) is kept simple.
Classpath
Now that we have our directories created and the package defined, we can define the classpath. The classpath is a variable that tells Java where to look for user-defined classes and packages. If we had a package that was called cs235.javaproject and its class files were appropriately stored in F:\cs235\javaproject\ , then we would want to define the classpath to be F:\ . In general, the classpath should point to the parent directory of the generalname.
For example, if we had a package called cs235.maze, then we could make our directory structure like F:\cs235\maze\ and our classpath would be F:\ .
As a practical example, if the student wanted to compile all the files in his cs235.javaproject package, and his files were contained in F:\cs235\javaproject\ he would first make the current directory the javaproject directory (if this is unclear see the DOS page). Then the student would enter the following command (assuming the student was using MS-DOS):
javac -classpath F:\ *.java
Then to run the TestDriver class inside that package he would use:
java -classpath F:\ cs235.javaproject.TestDriver
In Linux, the ~ (home directory) would replace the F:\ in compiling and running in this case.