Like most things, something is only complex if you don't understand it fully.
Perhaps it's best to compare classpath to your PATH environment variable on windows. If you want to be able to run programs/commands from the command line in windows, the command shell needs to know where to look for a particular command. For example, a common command is "ping". When you type "ping" the command shell looks for a program named ping.exe in all the directories you have specified in the PATH variable. If you remove the system32 directory from your path and type "ping", the command shell won't be able to locate this prgram, thus won't be able to run it for you.
Classpath is a similar concept in
java. It provides a list of directories or jar files that the java tools can look in to find classes or resources. If you ask a java tool, such as javac or just the java.exe program to use a class, but haven't specified *where* to find that class, java can't find it and won't run it or compile it.
Every java tool needs the classpath specified. If you don't manually specify one with the -classpath setting, java tools will try to look at the defined environment variables for one named CLASSPATH and use that value.
So if you try to compile something like:
javac HelloWorld.java
But haven't told the javac program
where to find the HelloWorld.java class, you're going to get a error: cannot read: HelloWorld.java.
If you try to run a java class like HelloWorld but don't specify a classpath , you're going to get: Exception in
thread "main" java.lang.NoClassDefFoundError: HelloWorld.
This is no different than if you try to run a command in the command shell but haven't provide a PATH setting for that command. You'll get an error saying that it is not recognized as an internal or external command,
operable program or batch file.
[ March 04, 2002: Message edited by: Rob Ross ]