| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs Java FAQ:
How to set up and use system classpath (CLASSPATH)?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
How to setup and use system classpath (CLASSPATH)?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 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 orderThe 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 CLASSPATHIn 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 XPTo configure a persistent CLASSPATH under Windows XP:
Combine CLASSPATH with "-classpath" optionIt is easy to overlook that the On Unix: javac -classpath $CLASSPATH:dir1:dir2 ... where 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 ... |