Wednesday, December 10, 2008

Installing and Using

Chapter 2: Installing and Using Java Tools

Java development environments have two basic approaches. On the one hand, you can use a sophisticated Integrated Development Environment (IDE) such as Sun's Java Studio Creator, IntelliJ IDEA, or Eclipse. These tools combine a full-featured source editor that lets you edit your Java program files with integrated development tools, including visual development tools that let you create applications by dragging and dropping visual components onto a design surface.

At the other extreme, you can use just the basic command-line tools that are available free from Sun's Java Web site (http://www.java.sun.com). Then you can use any text editor you wish to create the text files that contain your Java programs (called source files), and compile and run your programs by typing commands at a command prompt.

Tip As a compromise, you may want to use a simple development environment, such as TextPad. TextPad is an inexpensive text editor that provides some nice features for editing Java programs (such as automatic indentation) and shortcuts for compiling and running programs. However, it doesn't generate any code for you or provide any type of visual design aids. TextPad is the tool I used to develop all the examples shown in this book. For information about downloading and using TextPad, refer to Book I, Chapter 3. Eclipse is a free, open-source development environment that's gaining popularity. I describe it in Book I, Chapter 4.


Downloading and Installing the Java Development Kit
Before you can start writing Java programs, you have to download and install the correct version of the Java Development Kit (JDK) for the computer system you're using. Sun's Java Web site provides versions for Windows, Solaris, and Unix. The following sections show you how to download and install the JDK.

Downloading the JDK
To get to the download page, point your browser to java.sun.com/javase/downloads. Then follow the appropriate links to download the JDK 6 for your operating system.

TECHNICAL STAUFF Presently a menu of Popular Downloads is on the right side of Java's home page at http://www.java.sun.com. At the top of that menu is a link to the download site for the current version of Java. So, if you don't want to type an entire link, you can just go to http://www.java.sun.com and then use the Popular Downloads list to navigate quickly to the appropriate download page.


When you get to the Java download page, you find links to download the JDK or the JRE. Follow the JDK link; the JRE link gets you only the Java Runtime Environment, not the complete Java Development Kit.

The JDK download comes in two versions: an online version that requires an active Internet connection to install the JDK, and an offline version that lets you download the JDK installation file to your disk, then install it later. I recommend you use the offline version. That way you can reinstall the JDK if you need to, without having to download it again.

The exact size of the offline version depends on the platform, but most of them are between 50MB and 60MB. As a result, the download takes a few hours if you don't have a high-speed Internet connection. With a cable, DSL, or T1 connection, the download takes less than five minutes.

Installing the JDK
After you download the JDK file, you can install it by running the executable file you downloaded. The procedure varies slightly depending on your operating system, but basically you just run the JDK installation program file after you download it:

On a Windows system, open the folder to which you saved the installation program and double-click the installation program's icon.

For a Linux or Solaris system, use console commands to change to the directory to which you downloaded the file, and then run the program.



Legal mumbo jumbo
Before you can download the JDK, you have to approve of the Java license agreement, all 2,463 words of it including the thereupons, whereases, and hithertos all finely crafted by Sun's legal department. I'm not a lawyer (and I don't play one on TV), but I'll try to summarize the license agreement for you:

Sun grants you the right to use Java as is, and doesn't promise that it will do anything at all.

The party of the second part (that's you) in turn promises to use Java only to write programs. You're not allowed to try to figure out how Java works and sell your secrets to Microsoft.

You can't use Java to run a nuclear power plant. (I'm not making that up. It's actually in the license agreement.)





After you start the installation program, it asks any questions it needs to know to properly install the JDK. You're prompted for information such as which features you want to install and what folder you want to install the JDK to. You can safely choose the default answers for each of the options.

Perusing the JDK folders
When the JDK installs itself, it creates several folders on your hard drive. The locations of these folders vary depending on your system, but in Windows the JDK root folder is found under Program Files\Java on your boot drive. The name of the JDK root folder also varies, depending on the exact Java version you've installed. For version 1.6, the root folder is named jdk1.6.0 (or something similar, such as jdk1.6.0_02).

Table 2-1 lists the subfolders created in the JDK root folder. As you work with Java, you'll frequently refer to these folders.

Table 2-1: Folders in the JDK Root Folder
Open table as spreadsheet Folder
Description

bin
The compiler and other Java development tools.

demo
Demo programs you can study to learn how to use various Java features.

docs
The Java API documentation. (For instructions on how to create this folder, see the section "Using Java Documentation" later in this chapter.)

include
This library contains files needed to integrate Java with programs written in other languages.

jre
The Runtime Environment files.

lib
Library files, including the Java API class library.

src
The source code for the Java API classes. This folder is only created if you unpack the src.zip file (this file may be named src.jar). After you get your feet wet with Java, looking at these source files can be a great way to learn more about how the API classes work.


In addition to these folders, the JDK installs several files into the JDK root folder. I list these files in Table 2-2.

Table 2-2: Files in the JDK Root Folder
Open table as spreadsheet File
Description

README.html
The Java readme file in HTML format.

README.txt
The readme file again, this time in text format.

LICENSE
The Java license that you agreed to when you downloaded the JDK, on the outside chance you enjoyed it so much the first time you want to read it again. (If you work for Microsoft, you probably should read it again, at least twice.)

LICENSE.rtf
The license file once again, this time in RTF format. (RTF is a document format that can be understood by most word processing programs.)

COPYRIGHT
Most companies are happy to just say (c) 2007 Sun Microsystems, Inc. at the bottom of the readme file or in the license file. But not Sun. It puts the copyright notice in a separate text file, along with information about all the copyright and export laws that apply.

Warning I guess the Java license you have to agree to at least twice-once when you download the JDK, and again when you install it-isn't clear enough about what you're not allowed to use Java for. The license says you can't use it for nuclear power applications. But the copyright notice (in the COPYRIGHT file) also prohibits you from using it in missile systems or chemical or biological weapons systems. If you work for the Defense Department, you'd better read the copyright notice!


Setting the path
After you install the JDK, you need to configure your operating system so that it can find the JDK command-line tools. To do that, you must set the Path environment variable. This variable is a list of folders that the operating system uses to locate executable programs. To do this on a Windows XP or Windows Vista system, follow these steps:

Open the Control Panel and click the Performance and Maintenance link (in Windows XP) or the System and Maintenance link (in Windows Vista).

Click the System link.

The System Properties dialog box comes up.

Click the Advanced tab (in Windows XP) or the Advanced System Settings link (in Windows Vista). Then click the Environment Variables button.

The Environment Variables dialog box, as shown in Figure 2-1, appears.


Figure 2-1: The Environment Variables dialog box.

In the System Variables list, select Path, and then click the Edit button.

A little dialog box comes up to let you edit the value of the Path variable.

Add the JDK bin folder to the beginning of the Path value.

Use a semicolon to separate the bin folder from the rest of the information that may already be in the path.Note: The exact name of the bin folder may vary on your system. For example:

c:\Program Files\Java\jdk1.6.0\bin;other directories...

Click OK three times to exit.

The first OK gets you back to the Environment Variables dialog box. The second OK gets you back to the System Properties dialog box. And the third OK closes the System Properties dialog box.

For earlier versions of Windows (such as ancient Windows 98 or Me), you set the path by adding a Path statement to the AutoExec.bat file in the root directory of your C drive. Here's an example:

path c:\Program Files\Java\jdk1.6.0\bin;other directories...

For Linux or Solaris, the procedure depends on which shell you're using. Consult the documentation for the shell you're using for more information.

No comments:

Post a Comment