CLASSPATH Environment Variable


The Java runtime has to know where to locate the .class files associated with an import declaration. The search paths are defined in the CLASSPATH environment variable. Don't worry about setting the CLASSPATH variable if you are using the unnamed package; the system will know to search your current working directory for the required files. You also don't have to worry about setting the CLASSPATH variable when working with the built-in libraries that come with the Java SDK.

You do have to change the CLASSPATH variable if you want to freely access the contents of user -defined packages. If you add the root directory of your package library to the CLASSPATH variable, you can freely access your libraries anywhere on your computer. If you don't add the package root directory to CLASSPATH, you will need to place your code development directory directly above the package directories.

Setting the CLASSPATH environment variable is system-dependent. On computers running Windows, you can modify the variable by editing the AUTOEXEC.BAT file. For example, if the C:/Books/Technical_Java directory were the root directory of the previously described Fluids.Gas package, you would set your CLASSPATH definition to the following ”

 SET CLASSPATH = .;C:/Books/Technical_Java 

The period in the CLASSPATH indicates that the system will search the current working directory for .class files. If you are running on a Unix system in csh you can change the CLASSPATH environment variable using the setenv command.

 setenv CLASSPATH .;C:/Books/Technical_Java 

In sh, the CLASSPATH variable can be changed with the following command ”

 CLASSPATH = .;C:/Books/Technical_Java export CLASSPATH 

Another way to set the search path for .class files (and the one recommended at the Sun Java website) is by using the “classpath compiler and JVM options. This approach allows you to customize the CLASSPATH for a given application without affecting other applications, although it can be somewhat tedious if you have to specify a large number of paths.

Example: Using CLASSPATH to Locate .class Files

Add the directory containing the SpeciesDriver.java program and the Fluids directory to your CLASSPATH environment variable list. You can now compile the SpeciesDriver.java program anywhere on your system.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

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