Setting the Classpath


set CLASSPATH = /user/projects/classes java classpath =     CLASSPATH%;classes/classa.class;libs/stuff.jar



The classpath is used by the java executable and the java compiler to find the compiled class files and any libraries packaged as JAR files required to run or compile a program. JAR files are the standard way of packaging libraries into a single file resource. The preceding phrase shows how the classpath can be set when executing a java program at the command line. By default, the classpath is obtained from the operating system CLASSPATH environment variable. In the phrase, a specific class, classfile.class, located in the classes directory, is appended to the classpath set by the environment variable. A library called stuff.jar, located in the libs directory, is also appended to the classpath.

If the CLASSPATH environment variable is not set, and the classpath option is not used, the classpath defaults to the current directory. If you do set the classpath using either of these options, the current directory is not automatically included in the classpath. This is a common source of problems. If you are setting the classpath, you must explicitly add the current directory. You can add the current directory to the classpath by specifying it as "." in the classpath.

Caution

Note that while any classes are found in a directory that is included in the classpath, JAR files must be explicitly included in the classpath to be found. They will not be found just by including the directory in which they reside, in the classpath.


Classpath-related problems are very common among novice and even experienced programmers, and can often be very frustrating to solve. If you take the time to fully understand the classpath and how to set it, then you should be able to avoid these types of problems in your applications. For additional information about setting and using the classpath, see this URL - http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/classpath.html.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net