Java Reference
In-Depth Information
you don't set the classpath, or you set it incorrectly, Java is not able to find the classes in any new packages
you might create. If you omit the period from the
-classpath
string when executing your program, you get
a message to the effect that
main()
cannot be found and your program does not run.
Another way to make your packages available after you have compiled them is by making them
exten-
sions
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 de-
fault directory structure that is created is shown in
Figure 5-9
.
The classes and packages in the .
jar
archives that you place in the
ext
directory are automatically ac-
cessible when you compile or run your Java programs without the need to set the
CLASSPATH
environment
variable or use the
-classpath
command-line option. When you create a
.jar
file for a package, you need
to make sure that you add the
.class
files with the directory structure corresponding to the package name
— you can't just add the
.class
files to the archive. For example, suppose you want to store the
Geometry
package in an archive. Assuming you have already compiled the package and the current directory contains
the package directory, you can use the following command to create the archive:
C:\Beg Java Stuff>jar cvf Geometry.jar Geometry\*.class
This creates the archive
Geometry.jar
and adds all the
.class
files that are in the
Geometry
directory
to it. All you now need to do to make the package available to any program that needs it is to copy it to the
ext
directory in the JDK directory hierarchy shown in
Figure 5-9
.
The
jar
utility does a lot more than I have described here. If you want to know more about what it can
do, look into the “Tools and Utilities” section of the JDK documentation.