Core Java(TM) 2, Volume I--Fundamentals (7th Edition) (Core Series) (Core Series)

   

The most complete and up-to-date versions of the Java 2 Standard Edition (J2SE) are available from Sun Microsystems for Solaris, Linux, and Windows. Versions in various states of development exist for the Macintosh and many other platforms, but those versions are licensed and distributed by the vendors of those platforms.

Downloading the JDK

To 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 occasional references to the old term.

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 fractional version number increment did not properly communicate the momentous advances of JDK 1.2. However, because they had that insight only after the release, they decided to keep the version number 1.2 for the development kit. Subsequent releases were numbered 1.3, 1.4, and 5.0. The platform, however, was renamed from "Java" to "Java 2." Thus, we have Java 2 Standard Edition Development Kit version 5.0, or J2SE 5.0.

For engineers, all of this might be a bit confusing, but that's why we never made it into marketing.

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 eager beavers in marketing will call it when you approach the Sun web site. We suggest that you install only the Java Development Kit at this time. If you later decide to use Sun's integrated development environment, simply download it from http://netbeans.org.

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

The setup procedure offers a default for the installation directory that contains the JDK version number, such as jdk5.0. This sounds like a bother, but we have come to appreciate the version number it makes it easier to install a new JDK release for testing.

Under WIndows, we strongly recommend that you do not accept a default location with spaces in the path name, such as c:\Program Files\jdk5.0. Just take out the Program Files part of the path name.

In this book, we refer to the installation directory as jdk. For example, when we refer to the jdk/bin directory, we mean the directory with a name such as /usr/local/jdk5.0/bin or c:\jdk5.0\bin.

Setting the Execution Path

After 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.

  • In UNIX (including Solaris and Linux), the procedure for editing the execution path depends on the shell that you are using. If you use the C shell (which is the Solaris default), then add a line such as the following to the end of your ~/.cshrc file:

    set path=(/usr/local/jdk/bin $path)

    If you use the Bourne Again shell (which is the Linux default), then add a line such as the following to the end of your ~/.bashrc or ~/.bash_profile file:

    export PATH=/usr/local/jdk/bin:$PATH

    For other UNIX shells, you'll need to find out how to carry out the analogous procedure.

  • Under Windows 95/98/Me, place a line such as the following at the end of your c:\autoexec.bat file:

    SET PATH=c:\jdk\bin;%PATH%

    Note that there are no spaces around the =. You must reboot your computer for this setting to take effect.

  • Under Windows NT/2000/XP, start the control panel, select System, then Environment. Scroll through the User Variables window until you find a variable named PATH. (If you want to make the Java tools available for all users on your machine, use the System Variables window instead.) Add the jdk\bin directory to the beginning of the path, using a semicolon to separate the new entry, like this:

    c:\jdk\bin;other stuff

    Save your settings. Any new console windows that you start have the correct path.

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 double-check your installation.

Installing the Library Source and Documentation

The 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:

1.

Make sure that the JDK is installed and that the jdk/bin directory is on the execution path.

2.

Open a shell window.

3.

Change to the jdk directory (e.g., cd /usr/local/jdk5.0 or cd c:\jdk5.0).

4.

Make a subdirectory src

mkdir src cd src

5.

Execute the command:

jar xvf ../src.zip

(or jar xvf ..\src.zip on Windows)

TIP

The src.zip file contains the source code for all public libraries. To obtain even more source (for the compiler, the virtual machine, the native methods, and the private helper classes), go to http://www.sun.com/software/communitysource/j2se/java2/index.html.

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:

1.

Make sure that the JDK is installed and that the jdk/bin directory is on the execution path.

2.

Download the documentation zip file and move it into the jdk directory. The file is called j2sdkversion-doc.zip, where version is something like 5_0.

3.

Open a shell window.

4.

Change to the jdk directory.

5.

Execute the command

jar xvf j2sdkversion-doc.zip

where version is the appropriate version number.

Installing the Core Java Program Examples

You should also install the Core Java program examples. You can download them from http://www.phptr.com/corejava. The programs are packaged into a zip file corejava.zip. You should unzip them into a separate directory we recommend you call it CoreJavaBook. You can use any zip file utility such as WinZip (http://www.winzip.com), or you can simply use the jar utility that is part of the JDK. If you use jar, do the following:

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

Navigating the Java Directories

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.

Table 2-1. Java 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 curious about the inner workings of the System class, you can look inside src/java/lang/System.java.


       

    Категории