Exception in thread "main" java.lang.NoClassDefFoundError: Gerald
OK - so where did Java look, and where should it have looked?
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 08:33:36)
Associated topics are indexed under
J708 - Java - Packages [2865] Relationships between Java classes - inheritance, packaging and others - (2010-07-10)
[2114] Which Version of Java am I running? - (2009-04-02)
[754] tar, jar, war, ear, sar files - (2006-06-10)
J709 - Java - Class Access [2535] When should I use Java, Perl, PHP, or Python? - (2009-12-13)
[874] Who can use which access door? - (2006-09-21)
Some other Articles
What is a JVM, a JRE, a JDK - components of the core Java EnvironmentLooking inside Java classes - javap and javadocSorting Collections of Objects in JavaExceptions in Java - why and howWhere is my Java class?Viv.java uses unchecked or unsafe operations - explanation and cureJava Programming FundamentalsAutomating access to a page obscured behind a holding pageVariable names like i and j - why?Hello World - a good traditional start to a Java course