Java Reference
In-Depth Information
The CLASSPATH environment variable is a frequent source of problems and confusion to newcomers
to Java. The current SDK does NOT require CLASSPATH to be defined, and if it has been defined by
some other Java version or system, it is likely to cause problems. Commercial Java development systems
and versions of the Java Development Kit prior to 1.2 may well define the CLASSPATH environment
variable, so check to see whether CLASSPATH has been defined on your system. If it has and you no
longer have whatever defined it installed, you should delete it. If you have to keep the CLASSPATH
environment variable - maybe because you want to keep the system that defined it or you share the
machine with someone who needs it - you will have to use a command line option to define
CLASSPATH temporarily whenever you compile or execute your Java code. We will see how to do this
a little later in this chapter.
Extracting the Sourcecode for the Class Libraries
The sourcecode for the class libraries is included in the archive src.zip that you will find in the jdk1.4
root directory. Browsing this source can be very educational, and it can also be helpful when you are more
experienced with Java in giving a better understanding of how things works - or when they don't, why they
don't. You can extract the source files from the archive using the Winzip utility or any other utility that will
unpack .zip archives - but be warned - there's a lot of it and it takes a while!
Extracting the contents of src.zip to the root directory \jdk1.4 will create a new subdirectory, src ,
and install the sourcecode in subdirectories to this. To look at the sourcecode, just open the .java file
that you are interested in, using any plain text editor.
Compiling a Java Program
Java sourcecode is always stored in files with the extension .java . Once you have created the
sourcecode for a program and saved it in a .java file, you need to process the source using a Java
compiler. Using the compiler that comes with the JDK, you would make the directory that contains
your Java source file the current directory, and then enter the following command:
javac -source 1.4 MyProgram.java
Here, javac is the name of the Java compiler, and MyProgram.java is the name of the program
source file. This command assumes that the current directory contains your source file. If it doesn't the
compiler won't be able to find your source file. The -source command line option with the value 1.4
here tells the compiler that you want the code compiled with the SDK 1.4 language facilities. This
causes the compiler to support a facility called assertions , and we will see what these are later on. If you
leave this option out, the compiler will compile the code with SDK 1.3 capabilities so if the code uses
assertions, these will be flagged as errors.
If you need to override an existing definition of the CLASSPATH environment variable - perhaps
because it has been set by a Java development system you have installed, the command would be:
javac -source 1.4 -classpath . MyProgram.java
The value of CLASSPATH follows the -classpath specification and is just a period. This defines just
the path to the current directory, whatever that happens to be. This means that the compiler will look
for your source file or files in the current directory. If you forget to include the period, the compiler will
not be able to find your source files in the current directory. If you include the -classpath .
command line option in any event, it will do no harm.
Search WWH ::




Custom Search