Chapter 2. The Java Programming Environment
In this chapter, you will learn how to install the Java Development Kit (JDK) and how to compile and run various types of programs: console programs, graphical applications, and applets. You run the JDK tools by typing commands in a shell window. However, many programmers prefer the
|
|
|
Installing the Java Development Kit
The most complete and up-to-date versions of the Java 2 Standard Edition (J2SE) are available from Sun Microsystems for Solaris, Linux, and
Downloading the JDKTo download the Java Development Kit, you will need to navigate the Sun web site and decipher an amazing amount of jargon before you can get the software that you need.
You already saw the abbreviation JDK for Java Development Kit. Somewhat confusingly, versions 1.2 through 1.4 of the kit were known as the Java SDK (Software Development Kit). You will still find
Next, you'll see the term "J2SE" everywhere. That is the "Java 2 Standard Edition," in contrast to J2EE (Java 2 Enterprise Edition) and J2ME (Java 2 Micro Edition).
The term "Java 2" was coined in 1998 when the marketing folks at Sun felt that a
For
If you use Solaris, Linux, or Windows, point your browser to http://java.sun.com/j2se to download the JDK. Look for version 5.0 or later, and pick your platform.
Sometimes, Sun makes available bundles that contain both the Java Development Kit and an integrated development environment. That integrated environment has, at different times of its life, been named Forte, Sun ONE Studio, Sun Java Studio, and Netbeans. We do not know what the
After downloading the JDK, follow the platform-dependent installation directions. At the time of this writing, they were available at http://java.sun.com/j2se/5.0/install.html. Only the installation and compilation instructions for Java are system dependent. Once you get Java up and running, everything else in this book should apply to you. System independence is a major benefit of Java.
NOTE
Setting the Execution PathAfter you are done installing the JDK, you need to carry out one additional step: add the jdk /bin directory to the execution path, the list of directories that the operating system traverses to locate executable files. Directions for this step also vary among operating systems.
Here is how you test whether you did it right: Start a shell window. How you do this depends on your operating system. Type the line java -version and press the ENTER key. You should get a display such as this one: java version "5.0" Java(TM) 2 Runtime Environment, Standard Edition Java HotSpot(TM) Client VM
If instead you get a message such as "java: command not found", "Bad command or file name", or "The name specified is not recognized as an internal or external command, operable program or batch file", then you need to go back and
Installing the Library Source and DocumentationThe library source files are delivered in the JDK as a compressed file src.zip , and you must unpack that file to get access to the source code. We highly recommend that you do that. Simply do the following:
TIP
The documentation is contained in a compressed file that is separate from the JDK. You can download the documentation from http://java.sun.com/docs. Several formats (.zip, .gz, and .Z) are available. Choose the format that works best for you. If in doubt, use the zip file because you can uncompress it with the jar program that is a part of the JDK. Simply follow these steps:
Installing the
|
|
1. |
Make sure the JDK is installed and the
jdk
/bin
directory is on the execution path.
|
|
2. |
Make a directory
CoreJavaBook
.
|
|
3. |
Download the
corejava.zip
file to that directory.
|
|
4. |
Open a shell window.
|
|
5. |
Change to the
CoreJavaBook
directory.
|
|
6. |
Execute the command:
jar xvf corejava.zip |
In your explorations of Java, you will occasionally want to peek inside the Java source files. And, of course, you will need to work extensively with the library documentation. Table 2-1 shows the JDK directory tree.
|
Directory Structure |
Description |
|---|---|
|
|
(The name may be different, for example, jdk5.0 ) |
|
The compiler and tools |
|
|
Look here for demos |
|
|
Library documentation in HTML format (after expansion of j2sdkversion-doc.zip ) |
|
|
Files for compiling native methods (see Volume 2) |
|
|
Java runtime environment files |
|
|
Library files |
|
|
The library source (after expanding src.zip ) |
The two most useful subdirectories for learning Java are docs and src . The docs directory contains the Java library documentation in HTML format. You can view it with any web browser, such as Netscape.
TIP
|
|
Set a bookmark in your browser to the file docs/api/index.html . You will be referring to this page a lot as you explore the Java platform. |
The
src
directory contains the source code for the public part of the Java libraries. As you become more comfortable with Java, you may find yourself in situations for which this book and the on-line information do not provide what you need to know. At this point, the source code for Java is a good place to begin digging. It is reassuring to know that you can always dig into the source to find out what a library function really does. For example, if you are
|
|