When 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.
(written 2008-11-26)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J601 - Java Introduction [25] Release numbers - (2004-08-23)
[111] Training notes available under Open Distribution license - (2004-11-07)
[124] PHP v Java - (2004-11-20)
[317] Programming languages - a comparison - (2005-05-20)
[792] Is Java the right language to learn? - (2006-07-04)
[871] Java oversold? - (2006-09-19)
[1049] Java 6, Apache Tomcat 6. - (2007-01-21)
[1158] Private Java Course - A customer's pictures - (2007-04-22)
[1418] A Golf Club Decision - Perl to Java - (2007-11-01)
[1466] Effective Java training - the bootcamp approach - (2007-12-09)
[1497] Training Season Starts again! - (2008-01-07)
[1557] Trying out our Java examples on our web site - (2008-02-27)
[1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
[2115] Finding your java program - the CLASSPATH variable - (2009-04-02)
[2423] What is a JVM, a JRE, a JDK - components of the core Java Environment - (2009-09-26)
[2536] All the Cs ... and Java too - (2009-12-13)
[4332] First Java Application - calculating the weight of a tablecloth - (2014-11-29)
A912 - Web Application Deployment - Upgrading and tuning Tomcat [1377] Load Balancing with Apache mod_jk (httpd/Tomcat) - (2007-10-02)
[1718] Increasing Java Virtual Machine memory for Tomcat - (2008-07-24)
[2060] Database connection Pooling, SSL, and command line deployment - httpd and Tomcat - (2009-03-01)
[2082] Jmeter - a first test case - (2009-03-14)
[3018] Tuning Apache httpd and Tomcat to work well together - (2010-10-27)
A503 - Web Application Deployment - Java - Sourcing, Installing, Initial Testing [2079] Java - Memory Allocation and garbage collection - (2009-03-14)
[2080] Using ApacheBench and jconsole to test and monitor Tomcat - (2009-03-14)
[2153] Class Loading and Variable Conversion in Java - (2009-05-02)
[2184] Choosing the right version of Java and Tomcat - (2009-05-16)
Some other Articles
Book now for 2009Remember Me - PHPJava - Genericsjavax.servlet cannot be resolved - how to solveJava CLASSPATH explainedMelksham Hotel RatesLong, Longer, Longest in JavaA Gold Star for First Great Western Customer ServiceRuby, Perl, Linux, MySQL - some training notesdaemons - what is running on my Linux server?