Java looks for classes in each of the locations in the CLASSPATH environment variable in turn - each of those locations being either a directory or a .jar file which contains a directory structure.

Within Java classes, import statements may be added which will have Java look in those extra locations relative to each member of the CLASSPATH. Take a look at this diagram:

With three elements on the CLASSPATH, and four imports in a class, further classes will be loaded from 3 * (4 + 1) = 15 different places [Don't forget that there's always an implicit import *; to load from current package!]. It's little wonder, then, that if you get your CLASSPATH and imports wrong that you'll be left asking "where did it load THAT form" or "why couldn't it find THAT?"

There's a further layer - as shown in the diagram - that classes and class members may be generally visible (i.e. public)or they may also be restricted - to protected, to package (the default and never stated) or private ... so that another reason that your class can't be loaded can be that it's there ... but the JVM's not allowed to use it.

While we're on packages and classes:

In essence, a "package" is a directory of code and a "class" is a file of code. The example shows some of the classes and packages that are provided with the standard Java Runtime Environment (JRE). They're all in the java package (extended ones in the enterprise edition are in javax), which has subpackages such as lang (general language stuff), io (input / output) and util (utilities). Withing java.lang you have a whole lot of classes such as String - or its full name java.lang.String ... in this case, Strings handling unicode character strings. (written 2009-09-24)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles

This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.