Using Classpath

The classpath is an environment variable that's used by the Java compiler and the Java virtual machine for class definitions.

You can use it in your work and in conjunction with examples presented in the text. Here is an example

 

Suppose you are trying the application, BagDemonstration.java, from the text that works with the IntArrayBag class. That imports the packages IntArrayBag and EasyReader as follows

   // FILE: BagDemonstration.java
   // This small demonstration program shows how to use the IntArrayBag class
   // from the edu.colorado.collections package.
import edu.colorado.collections.IntArrayBag; import edu.colorado.io.EasyReader; public class BagDemonstration {

You know the compiler will be looking for the classes IntArrayBag and EasyReader in a directory that includes edu/colorado/collections and edu/colorado/io as subdirectories. However, if we can set classpath to the name of a directory so the compiler will expect to find edu/collections as all its subdirectories there. These happen to be in /users/ernie/321 so you can use them by either of the two methods

  1. You only need to do this once. Set the environment variable CLASSPATH.
  2. You need to do this every time you compile or execute a java program. Set the value of classpath as an option on the command line.


 

for compiling and executing programs written in Java


Creative Commons License
This work is licensed under a Creative Commons License.
Ernest Ackermann Department of Computer Science, Mary Washington College
CPSC 104 | CPSC 220 | CPSC 321
This page was last modified on .