Java Reference
In-Depth Information
It should work fine this time. (If not, make sure that your CLASSPATH is set
correctly.) Notice the -d flag created the directory \programs in the \com\
wiley directory, then output the CarDealer.class file there.
Now that both the Vehicle and CarDealer classes are compiled, try running
the CarDealer program by entering the command:
java CarDealer
You should get the following error message:
Exception in thread “main” java.lang.NoClassDefFoundError: CarDealer
The JVM cannot find the CarDealer class. The reason is not a CLASSPATH
problem because we set the CLASSPATH environment variable to include
c:\my_bytecode, which is the correct directory. The reason the CarDealer class
cannot be found is that we are using the wrong name.
The CarDealer class is in the com.wiley.programs package, so the name of
the class is com.wiley.programs.CarDealer. Try entering the command:
java com.wiley.programs.CarDealer
Figure 7.4 shows the output of running the CarDealer program. Notice that
you can run the CarDealer from any directory of the command prompt
because the JVM is not looking in the current directory but the CLASSPATH.
Do not include any part of the package directory structure in your CLASS-
PATH. For example, the compiler and JVM will not be able to find Vehicle or
CarDealer if your CLASSPATH is set to:
set CLASSPATH=c:\my_bytecode\com\wiley;
There are no situations where CLASSPATH should contain package directo-
ries. It should only contain the directories where the package directories can be
found. If you are having a problem with classes not being found, it is likely a
CLASSPATH problem. Be sure to check your CLASSPATH carefully for any
mistakes or typos.
Figure 7.4
Run the CarDealer program by using its fully qualified name.
Search WWH ::




Custom Search