Java Reference
In-Depth Information
■
the value for
CLASSPATH
is a list of directories, Jar files, and Zip files that are separated by a path-separator.
the path-separator is operating system dependent. on a Windows operating system, the separator is a semicolon.
on a UniX-like operating systems, for example, Linux, solaris, and mac os X, the separator is a colon (:). on Windows
operating system, a
CLASSPATH
entry looks like
C:\;C:\jbook;C:\jp;c:\myapps.jar
. on UniX-like operating systems,
it looks like
/usr/jp/classes:/usr/myapps.jar
.
Tip
You can also set
CLASSPATH
by typing the following command on command prompt. However, such
CLASSPATH
setting is valid only for the current command prompt session.
C:\>SET CLASSPATH=C:\;C:\jbook;C:\javaprograms
After you have set the
CLASSPATH
, you can use the following command to run the
Welcome
class:
C:\>java com.jdojo.intro.Welcome
As I have already discussed, the JVM will convert the class name to a class file,
com\jdojo\intro\Welcome.class
.
The JVM reads the
CLASSPATH
value, which it will use to locate the class file. It reads the first entry from the
CLASSPATH
value, which is
C:\
, and concatenates the converted class name string to it. The JVM may use an extra backslash if
needed to create a valid file name. In your case, the resulting string will be
C:\com\jdojo\intro\Welcome.class
The JVM checks if a file with the above path exists on the machine. In this case, this check will fail, because you
do not have a
C:\com\jdojo\intro\Welcome.class
file on your machine. If the JVM fails to locate a class file using
an entry in the
CLASSPATH
, it repeats the class file search using next entry from the
CLASSPATH
. This process continues
until the JVM locates the class file or it has exhausted all entries in the
CLASSPATH
. In your case, the JVM will find the
class file in the third attempt. It will find the
C:\javaprograms\com\jdojo\intro\Welcome.class
file. This is not the
end of the story. Since you passed the class name
com.jdojo.intro.Welcome
to the JVM, the JVM makes sure that the
C:\javaprograms\com\jdojo\intro\Welcome.class
was generated by a Java compiler for a class named
Welcome
,
whose package declaration was
com.jdojo.intro
. In your case, this criterion is fulfilled.
What does the JVM do next? Now, it looks for a method declaration in the
Welcome
class whose declaration must
look as follows:
public static void main (String[] args)
If the JVM does not find the
main
method declaration, or the
main
method is not declared static final, it prints an
error message and aborts the execution. This is the reason why you declared the
main
method in your
Welcome
class.
In your case, the JVM will find the correct declaration for the method main. It executes the body of the main method,
which prints the following message on the console:
Welcome to the Java world.
There is another way of setting the
CLASSPATH
. You can set the
CLASSPATH
when you run your Java class. You can
use
-cp
or
-classpath
option with the
java
command to set the
CLASSPATH
while running the class. For example, you
can run the
Welcome
class as follows. Note that the command is entered on the command line in one line, like.
C:\>java -cp C:\;C:\jbook;C:\javaprograms com.jdojo.intro.Welcome