Java CLASSPATH explained
Archive - Originally posted on "The Horse's Mouth" - 2008-11-26 07:06:00 - Graham EllisWhen you run a Java program (be it though the java virtual machine or as a servlet within a web or application container such as Tomcat), it's only the main code (class) that's initially loaded. Any other code (classes) are loaded in dynamically as needed.
Where are these classes loaded from?
They're loaded from EITHER directory structures OR jar files, and the locations of these are in part defaulted (for the standard stuff) and in part defined by the CLASSPATH - an environment variable in which you list the root of the tree in which the JVM (Java Virtual Machine) should start looking. Let's see an example:
export CLASSPATH=/home/demo/packages:/usr/local/tomcat/shared/lib/wellhouse.jar:.
will cause the JVM to start looking in the packages subdirectory of the demo user's home directory, in a shared directory in your Tomcat installation, and in your current directory.
But that's only where it will start looking. If you're calling in a class called (say) net.wellho.tomcourse.Animal, then the extra package names will be added on to the base path and the actual files looked for on your disc will be:
/home/demo/packages/net/wellho/tomcourse/Animal.class (an absolute path)
/usr/local/tomcat/shared/lib/wellhouse.jar ... within in, the file ./net/wellho/tomcourse/Animal.class
/net/wellho/tomcourse/Animal.class (i.e. below the current directory)
There are even more places that will be searched if you add some import statements to your Java class. Let's say you're loading from a class that includes:
import net.wellho.general.*;
import net.wellho.tomcourse.*;
import org.apache.tomcat.*;
then you'll end up looking in no less that 12 places if you simply call up a class called Animal using the earlier CLASSPATH example:
/home/demo/packages/Animal.class (an absolute path)
/home/demo/packages/net/wellho/general/Animal.class (an absolute path)
/home/demo/packages/net/wellho/tomcourse/Animal.class (an absolute path)
/home/demo/packages/org/apache/tomcat/Animal.class (an absolute path)
In /usr/local/tomcat/shared/lib/wellhouse.jar for Animal.class
In /usr/local/tomcat/shared/lib/wellhouse.jar for net/wellho/general/Animal.class
In /usr/local/tomcat/shared/lib/wellhouse.jar for net/wellho/tomcourse/Animal.class
In /usr/local/tomcat/shared/lib/wellhouse.jar for org/apache/tomcat/Animal.class
./Animal.class (below the current directory)
./net/wellho/general/Animal.class (below the current directory)
./net/wellho/tomcourse/Animal.class (below the current directory)
./org/apache/tomcat/Animal.class (below the current directory)
Remember too that the Java compiler (known as javac) checks your code - so that it needs access to all necessary classes on the machine you're developing / compiling on to ensure that you're only calling classes - and methods within them - that actually exist. So if you're writing a Servlet, for example, you'll need to have the Servlet API on your CLASSPATH on the development machine and in the development environment, as well as on the test and production servers.