--
Some utility code that you have developed or have obtained from a third party can usually be used in more than one Java program. These may sometimes be stored as packages, just as the Java utilities are in packages such as java.util.*. Whether or not they are packages or just a set of classes, you will want to store them in one place, separate from the directory where you are developing your program. Sometimes these libraries have a set of files stored in a single jar file (which is a file using zip format with a .jar extension). In order to make this work, the Java compiler and JVM must be able to find them.
Rather than specifying class search path on the
javac
command line by using the -classpath, we can make use of a 'system' class path. The CLASSPATH is an environment variable , in both Unix and Window systems, which will be used by both the Java compiler and the JVM to determine where to look for classes referenced by a Java program. Setting the system CLASSPATH is a useful procedure if you have JARs full of classes that you use all the time. The CLASSPATH environment variable can specify the location of classes in directories and in JAR files.For example, We have myJar1.jar and myJar2.jar files under myLibs directory. In Windows, the CLASSPATH environment variable should look like the following:
CLASSPATH=c:\myLibs\myJar1.jar;c:\myLibs\myJar2.jar;.
where the `.' indicates `current directory'. This includes the current directory in the search for classes.
Specification order
The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable.
What do not need to include in the CLASSPATH
In the CLASSPATH, you do not need to specify the location of normal J2SE packages and classes such as java.util or java.io.IOException.
You also do not need an entry in the CLASSPATH for packages and classes that you place in the ext directory (normally found in a directory such as C:\j2sdk\jre\lib\ext). Java will automatically look in that directory. So, if you drop your JAR files into the ext directory or build your directory structure off the ext directory, you will not need to do anything with setting the CLASSPATH. But this directory was intended for official Java extensions from Sun. Its use by unofficial Java extensions isn't recommended.
Example for Setting the CLASSPATH in Microsoft Windows XP
To configure a persistent CLASSPATH under Windows XP:
- Click the Start Button
- Select the Control Panel
- Click Switch to Classic View
- Click the System button
- Select the Advanced tab
- Click the Environment Variables button
- Click the New button to create a CLASSPATH or the Edit button to edit an existing CLASSPATH
- Enter CLASSPATH as the variable name
- Enter the list of directories or jar files in your CLASSPATH as the variable value, using a semicolon to separate entries in your list
- Click OK
- Click OK
- Click OK
Combine CLASSPATH with "-classpath" option
It is easy to overlook that the
-classpath
option on the command line replaces the default, system class path; it does not add to it. So what should I do if I want to set the class path to include the default system classpath plus some other entries? I could simply use the -classpath
option and list the default entries in addition to my extras. However, a better way is to reference the CLASSPATH environment variable. The syntax for this is different -- of course -- on Windows and Unix systems.On Unix:
javac -classpath $CLASSPATH:dir1:dir2 ...where
$CLASSPATH
expands to the current setting of the CLASSPATH environment variable.On Windows:
javac -classpath %CLASSPATH%;dir1;dir2 ...
Finally, please note that if directories in your class search path have spaces in their names, you may have to use double-quotes on the command line to prevent the CLASSPATH being split up. For example:
javac -classpath "%CLASSPATH%";dir1;dir2 ...
Comments
Post a Comment