Java Reference
In-Depth Information
FIGURE 1.3
The source code and bytecode are typically stored in separate folders.
•
c:\myproject\
•
+src\
•
| +com\
•
| +sybex\
•
| +demos\
•
| | +TestColors.java
•
| +events\
•
| +ColorChanger.java
•
+build\
•
+com\
•
+sybex\
•
+demos\
•
| +TestColors.class
•
+events\
•
+ColorChanger.class
A typical exam question at this point is to ask what the
CLASSPATH
needs to be for you to
run the
TestColors
program at the command prompt from any working directory. Do you
know the answer? I will reveal it in a moment, but fi rst here is the command prompt that
runs the
TestColors
application if you execute it from the
c:\myproject\build
directory:
java com.sybex.demos.TestColors
Notice the fully qualifi ed class name of
TestColors
must be specifi ed to execute
properly. Using the fully qualifi ed name has nothing to do with
CLASSPATH
or the current
working directory. The following command does not work and results in a
java.lang
.NoClassDefFoundError
, no matter what directory you run it from or what your
CLASSPATH
is set to
:
java TestColors
Why will this never work? Because there is no class called
TestColors
. Remember,
putting a class in a package changes the name of the class. Because
TestColors
is in the
com.sybex.demos
package, the name of the class is
com.sybex.demos.TestColors
, and that
name must be used on the command line.
By the way, the answer to the question earlier about
CLASSPATH
is it needs to contain
c:\myproject\build
:
set CLASSPATH=c:\myproject\build;
With this
CLASSPATH
, the command to run the
TestColors
program can be executed
from any directory.
Search WWH ::
Custom Search