The Java Classpath

September 5th, 2008

In my last post, I talked about how an operating system uses the PATH variable to determine which directories to search when running an executable. Java uses a similar concept when searching for classes.

Java provides the concept of a “class path”. This environment variable, appropriately named CLASSPATH, contains a list of directories in the same vein as the PATH variable. When the Java VM needs to find a class, it checks the directories in the class path, in order, for the class. The first class found that matches the package and name being requested is used.

This comes in handy when programs become more complex and classes span more than a single directory. But a more popular use of this is to access classes packaged in a JAR file. I’ll post more about both the uses of a JAR library as well as adding them to the class path in a future post.

Comments are closed.