Where is my Java class?
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)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesJ708 - Java - Packages 
Relationships between Java classes - inheritance, packaging and others - (2010-07-10) 
Which Version of Java am I running? - (2009-04-02) 
tar, jar, war, ear, sar files - (2006-06-10)J709 - Java - Class Access 
Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18) 
Private and Public - and things between - (2011-01-22) 
What is a universal superclass? Java / Perl / Python / Other OO languages - (2010-11-13) 
When should I use Java, Perl, PHP, or Python? - (2009-12-13) 
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
4487 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 at 50 posts per page
This is a page archived from The Horse's Mouth at
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.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).