The class path is a list of places to load classes.
These "locations" can be either directories or jar files.
For directories, the JVM will follow the expected pattern for loading the class. If I have a C: / myproject / classes directory in my class path and I try to load the com.mycompany.Foo class, it will look under the class directory for the com directory, then under this mycompany directory, and finally it will be look for a file named Foo.class in this directory.
In the second case, for jar files, it will look for a jar file for this class. A jar file is actually just a collection of directories like the ones above. If you unzip the jar file, you will get a bunch of directories and class files according to the template above.
So, the JVM traverses the class path from start to finish, looking for a class definition when trying to load a class definition. For example, in the classpath:
C: / MyProject / classes; C: /myproject/lib/stuff.jar; C: /myproject/lib/otherstuff.jar
First, the JVM will try to find the classes in the directory, then in stuff.jar and finally in otherstuff.jar .. p>
When you get a ClassNotFoundException, it means that the JVM went all the way to the classes and did not find the class you were trying to reference. The solution, as often in the Java world, is to test your class path.
You define the classpath on the command line by saying java -cp and then your classpath. In an IDE such as Eclipse, you will have a menu option to specify the class path.
user2000590 Jul 01 '13 at 16:20 2013-07-01 16:20
source share