Java Reference
In-Depth Information
Let's look at that before we go on to the second possibility.
With the .class files in the original package directory, either the path to your package must appear in
the string set for the CLASSPATH environment variable, or you must use the -classpath option on
the command line when you invoke the compiler or the interpreter. This overrides the CLASSPATH
environment variable if it happens to be set. Note that it is up to you to make sure the classes in your
package are in the right directory. Java will not prevent you from saving a file in a directory that is quite
different from that appearing in the package statement. Of the two options here, using the -classpath
option on the command line is preferable, because it sets the classpaths transiently each time, and can't
interfere with anything you do subsequently. In any event we will look at both possibilities.
If you elect to use the CLASSPATH environment variable, it only needs to contain the paths to your
packages. The standard packages supplied with Java do not need to be considered as the compiler and
the interpreter can always find them. For example, you might set it under Windows 95 or 98 by adding
the command,
set CLASSPATH=.;C:\MySource;C:\MyPackages
to your autoexec.bat file. Now the compiler and the interpreter will look for the directories
containing your packages in the current directory, as specified by the period, and the directories
C:\MySource and C:\MyPackages . Of course, you may have as many paths as you want defined in
CLASSPATH . They just need to be separated by semi-colons under Windows.
Under Unix, the equivalent to this might be:
CLASSPATH=.:/usr/local/mysource:/usr/local/mypackages
If you are using the Sun Java Development Kit, you also specify where your packages can be found by
using the -classpath option when you execute the Java compiler or the interpreter. This has the
advantage that it only applies for the current compilation or execution so you can easily set it to suit
each run. The command to compile MyProgram.java defining the classpath as in the environment
variable above would be:
javac -classpath ".;C:\MySource;C:\MyPackages" MyProgram.java
If you don't set the classpath in one of these ways, or you set it incorrectly, Java will not be able to find
the classes in any new packages you might create.
A new feature introduced with Java 2 provides you with a second way to make your packages available
once you have compiled them. This provides a way of adding extensions to the set of standard packages.
Using Extensions
Extensions are .jar files stored within the ext directory that is created when you install the JDK. The
default directory structure that is created is shown below.
Search WWH ::




Custom Search