Java Reference
In-Depth Information
the Jvm also allows you to use a forward slash in place of a dot in the fully qualified name of the class to run.
For example, you can also use the following command to run the Welcome class:
Tip
C:\javaprograms>java com/jdojo/intro/Welcome
When you use forward slashes in place of dots in the fully qualified name of the class to run, you must use only forward
slashes, irrespective of the operating system. For example, you cannot use backslashes on Windows and forward slashes
on UniX.
The second step the JVM takes is to append the word “.class” to the class name thus converted. The resulting
string out of the fully qualified name of the class becomes
com\jdojo\intro\Welcome.class
Now the JVM looks for the Welcome.class file, which must be under the intro directory, which in turn must
be under the jdojo directory, which in turn must be under the com directory. The JVM is not only looking for a
Welcome.class file. Rather, it is looking for a Welcome.class file in a directory structure like com\jdojo\intro .
You may realize that there could be many directory paths on your machine, which can lead the JVM to a
com\jdojo\intro\Welcome.class file. Suppose you have three Welcome.class files on your machine as follows:
C:\jp1\com\jdojo\intro\Welcome.class
C:\jp2\com\jdojo\intro\Welcome.class
C:\javaprograms\com\jdojo\intro\Welcome.class
How does the JVM know which one of the three Welcome.class files it has to run? Here comes the concept of
a mysterious (sometimes confusing and frustrating) creature called the CLASSPATH environment variable. The JVM
uses the CLASSPATH environment variable to locate a class file on your machine. Many questions about CLASSPATH
may have arisen in your mind. Who defines the CLASSPATH ? Where is it stored? How is it used? I will discuss all these
questions and their answers one by one.
The user of the machine sets the CLASSPATH . It can be set permanently, temporarily, or at runtime. If it is set
permanently, the JVM will use it whenever it needs to locate a class file. It can be set temporarily for the duration of a
command prompt session. It can be set at runtime by using -cp or -classpath option at the command line.
On Windows, the value of the CLASSPATH environment variable is a semicolon separated list of directories,
ZIP files, and JAR files. A typical setting for the CLASSPATH looks as follows:
SET CLASSPATH=C:\;C:\jbook;C:\javaprograms
On Windows, you can set the CLASSPATH environment variable using Settings Control Panel System
Advanced Tab Environment Variables button. It displays the Environment Variables dialog box shown in Figure 2-5 .
Under the System Variables group box, you should select CLASSPATH under the Variable column. If you do not find
CLASSPATH variable, it means that it has not been set previously. If the CLASSPATH variable has not been set previously,
click the New button. Otherwise, click the Edit button. Clicking the New or Edit button will display a dialog box as shown
in Figure 2-6 in which you can add/modify the value for the CLASSPATH .
 
 
Search WWH ::




Custom Search