Setting up Your Own Library of Utilities or Packages:
Using the CLASSPATH Environment Variable

Often you will want to regularly use some utility code that you have developed or have obtained from a third party in your Java programs. These may sometimes be set up 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 run programs must be able to find any of these.  This is done for command line use of Java and for some editors and IDEs by setting an environment variable called CLASSPATH. In Windows, the CLASSPATH environment variable should look like the following:


Note the "." at the end.  This includes the current directory in the search for classes.

Here we assume that we have a directory MyJavaLib on the C-drive that contains some utility classes or the directories that correspond to some packages.  That is the part before the first semi-colon.  The second part indicates that we have some classes stored in a file myutils.jar (these may just be classes or may include a directory structure for packages all that is within the jar file).  The third part indicates that we have a jar file blackboxclasses.jar.  This is another jar file, in this case one very similar to one that might be distributed with the Java Marine Biology Simulation Case Study.  Finally, after the last semi-colon we have the period, indicating that the current directory is on the CLASSPATH.  If some things are stored in directories that are subdirectories, the complete path, starting with "c:\" should be in the CLASSPATH.

Setting your CLASSPATH for Windows 95 or 98

Open the autoexec.bat file on the C-drive (c:\autoexec.bat) in an editor. Add the line

set CLASSPATH=c:\MyJavaLib;c:\MyJavaLib\myutils.jar;c:\MyJavaLib\blackboxclasses.jar;.

where the actual path is the one appropriate for your setup. Save and you are all set.  You will probably have to reboot the machine or at least log off and log on before it takes effect.  There is always a possibility on Win95 or Win98 that you will get an "out of environment space" message and the CLASSPATH will not be set.  In this case, open a DOS window by selecting it from the start menu.  Right click on the icon at the top left of the window and select properties.  Select memory, and set the environment to the highest setting (4096).  Then you should be fine.

Setting your CLASSPATH for Windows 2000 or XP

Right click on the "My Computer" icon.  Select Properties.  Select Advanced.  Click on Environment variables.  You probably want this set for the system, although you can do it for the user.  For the system, select New and in the Variable Name field type "CLASSPATH" and in the Variable Value window type the CLASSPATH as it appears above.  If you want to modify an existing CLASSPATH, just select it and click Edit.

You now can develop Java code as before, but your compiler and run commands will find the .java and .class files and packages in the CLASSPATH.


Chris Nevison
June 2002