Download Installing the Java Development Kit

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
Installing the Java Development Kit
If you run Windows, Linux or Solaris, then you can get the kit at the
oracle.com website. Macintosh users have a few other possibilities.
Java for Windows, Linux and Solaris
As I write this chapter, the exact URL for downloading the Java
Development Kit for Windows, Linux or Solaris is
www.oracle.com/technetwork/java/javase/downloads. I don't expect
that URL to work forever and ever, but if you visit oracle.com and poke
around for Java, you'll certainly reach the Java Development Kit download
page. One way or another, you should keep a few things in mind:
* Java comes in three separate editions.
A programming language is a bunch of rules describing the way you can
write instructions for the computer to follow. An application
programming interface (API) is a bunch of reusable code for performing
common tasks in a particular language. (Another name for an API is a
library.)
There's only one Java language, but the Java language has three official
APIs. When you download Java from oracle.com, you download some
Java language tools and one of the three Java APIs. Taken together, the
big bundle containing the language tools and one of the three APIs is
called an edition. The three available editions are as follows:
* Java Platform, Standard Edition (Java SE)
The Standard Edition has code for anything you can imagine doing
on a single desktop computer, and much more. This edition does
text-handling, mathematical calculations, input/output, collections
of objects, and much more.
To create this book's examples, you want the Java Platform,
Standard Edition.
* Java Platform, Enterprise Edition (Java EE)
The Enterprise Edition has code for things you do on an industrial strength server. The edition includes web server tools, s ophisticated
database tools, messaging between servers and clients, management
of systems, and the entire kitchen sink.
* Java Platform, Micro Edition (Java ME)
The Micro Edition has code for small devices, such as phones, TV
set-top boxes, and Blu-ray players. This edition has limited
capabilities that fit nicely into special-purpose devices that aren't as
powerful as today's computers.
© 2011 John Wiley & Sons, Inc.
2
<Remember>
To develop this book's Java programs, you want the Java Platform,
Standard Edition. If you already have Java's Enterprise Edition, and you
don't want more stuff on your hard drive, the Enterprise Edition is okay.
But the Enterprise Edition has much more than you need .
* Java comes in several different versions, with several updates to each
version.
Java's version numbering demonstrates what can happen when the
marketing department disrupts the timeline in the space-time continuum.
Java's earliest releases were numbered "1.0." Next came version "1.1",
then the strangely named "Java 2, version 1.2." The extraneous digit 2
hung around through "Java 2, version 1.3", "Java 2, version 1.4" and
finally "Java 2, version 5.0." (The spontaneous jump from 1.4 to 5.0 was
lots of fun.)
Next up was "Java 6" (with no extra 2 and no ".0"). After that is Java 7.
Each version is updated often, so a visit to oracle.com may offer Java
SE 7 Update 13 for download.
Any version of Java, starting with 5.0 and onward, is fine. Versions such
as 1.4 are not sufficient.
* Java has two kinds of downloads.
When you visit oracle.com, you see two acronyms floating around. You
see JRE (Java Runtime Environment) and JDK (Java Development Kit).
The JRE has everything you need in order to run existing Java programs.
Whether you know it or not, your desktop computer probably has a
version of the JRE.
The JDK has everything you need in order to run existing Java programs
and everything you need in order to create new Java programs. The JDK
has the entire JRE and more.
The download that you want is the JDK, which includes the JRE. You do
not want the JRE alone.
<Remember>
Download and install the Java JDK, not the Java JRE.
* Java might come with other tools.
A glance at the Java download page shows several options -- options to
download Java with NetBeans, JavaFX, the Java source code, the Java SE
documentation, and some other stuff. You might find the Java SE
documentation helpful, especially if you don't want to repeatedly visit
Oracle's online Java documentation. But the rest of the options (JavaFX
and others) don't help with plain, old Java development.
After you've downloaded the Java SE JDK, follow the instructions at
oracle.com for installing the software. On Windows, you normally double-
© 2011 John Wiley & Sons, Inc.
3
click a file with the .exe extension. On Linux, you install an .rpm file or
run a self-extracting .bin file. (I've never met a Linux geek who didn't
know what to do with these files.)
Those pesky filename extensions
On a Windows computer, the filenames displayed in My Computer or in Windows Explorer
can be misleading. You may visit your Downloads directory and see the name jdk-7u13windows-x64. Instead of just jdk-7u13-windows-x64, the file’s full name is jdk-7u13-windowsx64.exe. In other directories you may see two MyProgram files. What you don’t see is that
one file’s real name is MyProgram.java, and the other file’s real name is MyProgram.class.
The ugly truth is that My Computer and Windows Explorer can hide a file’s extensions. This
awful feature tends to confuse people. So, if you don’t want to be confused, modify the
Windows Hide Extensions feature. To do this, you have to open the Folder Options dialog
box. Here’s how:
*
In Windows XP with the control panel’s default (category) view: Choose
Start-->Control Panel-->Appearance and Themes-->Folder Options.
*
In Windows Vista or Windows 7 with the control panel’s default (category) view:
Choose Start-->Control Panel-->Appearance and Personalization-->Folder Options.
*
In Windows XP, Windows Vista or Windows 7 with the control panel’s classic
view: Choose Start-->Control Panel-->Folder Options.
In the Folder Options dialog box, click the View tab. Then look for the Hide File Extensions
For Known File Types option. Make sure that this check box is not selected.
Java for Macintosh
The first step is finding out if your system already has the Java Development
Kit. You have two choices:
* You can trust me on the subject of OS X version numbers.
Macintosh OS X 10.5 (codenamed "Leopard") and OS X 10.6
(codenamed "Snow Leopard") have the Java Development Kit preinstalled. If you have either version of OS X (possibly with more dots in
the version numbers, such as 10.6.4) then from the Java point of view,
you're good to go.
Macintosh OS X 10.7 (codenamed "Lion") comes without the Java
Development Kit. In fact, no version of the Java Development Kit is
formally blessed by Apple for OS X 10.7. So if you purchased OS X 10.7
thinking that you'd have the latest and the best, then you may have gotten
the latest, but (depending on your needs) you may not have gotten the
best. But don't despair. Instead, keep reading.
If you don't trust me about OS X version numbers (and frankly, you
shouldn't trust everything you find in print), you have another alternative:
© 2011 John Wiley & Sons, Inc.
4
* You can perform tests on your development computer to discover the
presence of a Java Development Kit, and of a development kit's
version number.
You have several choices for these tests. I list two choices in this chapter .
Run Java Preferences to determine your JDK version
Here's how you run the utility:
1. In the dock, select the Finder.
A Finder window opens.
2. In the Finder window's sidebar, select Applications.
A list of applications appears in the Finder window's main panel.
3. In the Finder window's main panel, double-click Utilities.
A list of utilities appears in the Finder window's main panel.
4. In the list of utilities, look for a Java Preferences entry.
If you don't find a Java Preferences entry, then don't fret (... not yet,
anyway). You might still have a Java Development Kit. Skip to the test
described in the next section.
5. If you have a Java Preferences entry, then double-click that entry.
After an uncomfortable delay, your computer displays a window
showing information about your computer's Java Development Kit. My
Mac displays the name Java SE 6 (along with the more cryptic version
number 1.6.0_22-b04-307). That's just fine.
<Remember>
To develop this book's sample progrems you need Java SE 5 (also
known as version number 1.5.0) or higher.
If the Java Preferences utility doesn't satisfy your needs, you can poke
around in Macintosh's UNIX command window. The next session tells you
how.
Issue a Terminal command to determine your JDK version
1. In the Spotlight search field, type the word Terminal.
2. When Terminal shows up as the Spotlight's top hit, press Enter.
A Terminal window opens (usually with plain black text on a plain
white background).
3. In the Terminal window type the following text, and then press
Enter:
javac -version
© 2011 John Wiley & Sons, Inc.
5
On my computer, the Terminal window responds with the following
text:
javac 1.6.0_22
If your computer responds with the number 1.5.0 or higher, then you
can pop open the champagne and look forward to some good times
developing Java programs.
<Warning>
The Macintosh Terminal presents a strange (sometimes confusing) mix
of case-sensitivity and "case-unsensitivity." For example, to see a list of
users, you can type either w or W on the Terminal command line. To see
a manual page describing that w command, you may type man w, MAN w,
or even mAn w. But if you type man W, you get the unfriendly No
manual entry for W response. In the same way, the javac -version
command requires all the letters in version to be lowercase. Typing the
incorrect javac -Version command gives you a disappointing
invalid flag: -Version message.
If your computer responds with something like command not found,
then you may be running Mac OS X 10.7.0 or later. If your computer
responds with a version number like 1.4.2 (or anything else that's less
than 1.5.0) then you may be running an older version of Mac OS X.
If your Mac doesn't have the Java JDK version 1.5.0 or
greater...
As usual, there are a few things you can try.
* In the Apple menu choose Software Update. In the resulting window look
for any update having to do with Java.
* Search for Java at developer.apple.com. Poke around and look for a
version of Java for your version of Mac OS X. Who knows? By the time
you read this book, the folks at Apple may have relented and added the
Java Development Kit back to OS X.
* Search other websites for Java and Mac OS X. If you're lucky, you'll find
a third-party Java Development Kit that's perfect for your system. (If
you're partially lucky, you'll find a JDK that's right for your system but is
difficult to install and works most of the time. Proceed with caution.) I've
found promising words at landonf.bikemonkey.org/static/soylatte
and at openjdk.java.net/projects/bsd-port, but I haven't
installed anything from these sites, so I can't vouch for them.
Installing the Eclipse Development Environment
An integrated development environment (IDE) is a program that provides
tools to help you create software easily and efficiently. You can develop
© 2011 John Wiley & Sons, Inc.
6
software (including Java programs) without an IDE, but the time and effort
you save using an IDE makes the IDE worthwhile. (Some hard -core
developers disagree with me, but that's another matter.)
According to the Eclipse Foundation's website, Eclipse is “a universal tool
platform — an open extensible IDE for anything and nothing in particular.”
Indeed, Eclipse is versatile. Most developers think of Eclipse as an IDE for
developing Java programs, but Eclipse has tools for developing in C++, in
PHP, and in many other languages. I've seen incarnations of Eclipse that
have nothing to do with program development. (I've even seen Lively
Browser @@md a Web browser whose tabs are built from Eclipse
components.)
Downloading Eclipse
Here's how you download Eclipse:
1. Visit www.eclipse.org.
2. Look for a way to download Eclipse for your operating system.
Today, I visit www.eclipse.org and see a big rectangle displaying the
words Get Started now... Download Eclipse. Tomorrow, who knows?
After clicking the Download Eclipse rectangle I see a dropdown box
with the names of three commonly used operating systems @@md
Windows, Linux, and Mac OS X. In this dropdown box I select the
operating system that's installed on my development computer.
3. Choose an Eclipse package from the available packages.
As I look at the Eclipse site today, I see Eclipse IDE for Java
Developers, Eclipse IDE for Java EE Developers, Eclipse Classic,
Eclipse IDE for C/C++ Developers, and others. You best bet is to select
Eclipse IDE for Java Developers.
4. Select 32-bit or 64-bit.
For most Eclipse packages you can choose between the 32 -bit version
and the 64-bit version. If you know that you're running a 64-bit
operating system, choose the 64-bit package. If you're running a 32-bit
operating system, or if you're not sure, choose the 32-bit package.
<Remember>
Your Java JDK and Eclipse versions should either both be 32 -bit or both
be 64-bit. As far as I know, Eclipse's 32-bit version isn't compatible
with Java's 64-bit version. So you can't mix and match these versions.
5. Follow the appropriate links to get the download to begin.
The links you follow depend on which of the many mirror sites is
offering up your download. Just wade through the possibilities and get
the download going.
© 2011 John Wiley & Sons, Inc.
7
Installing Eclipse
How the install Eclipse depends on your operating system and on what kind
of file you get when you download Eclipse. Here's a brief summary:
* If you run Windows and the download is an .exe file:
Double-click the .exe file's icon.
* If you run Windows and the download is a .zip file:
Extract the file's contents to the directory of your choice.
In other words, find the .zip file's icon in Windows Explorer. Then
double-click the .zip file's icon. (As a result, Windows Explorer displays
the contents of the .zip file, which consists of only one folder -- a folder
named eclipse.) Drag the eclipse folder to a convenient place in your
development computer's file system.
My favorite place to drag the eclipse folder is directly onto the C:
drive. So my C: drive has folders named Program Files, Windows,
eclipse, and others. I avoid making the eclipse folder be a subfolder of
Program Files, because from time to time I've had problems dealing
with the blank space in the name Program Files.
After copying the eclipse folder to a place on your hard drive, you can
run Eclipse by double-clicking the eclipse.exe file inside that folder.
* If you run Mac OS X:
If you download a .tar.gz file, find the file in your Downloads folder
and double-click it. Double-clicking the file should extract the file's
contents. After extraction, your Downloads folder contains a new Eclipse
folder. Drag this new Eclipse folder to your Applications folder, and your
all set.
If you download a .dmg file, your Web browser may open the file for
you. If not, then find the .dmg file in your Downloads folder and doubleclick the file. Follow any instructions that appear after this double-click.
If you're expected to drag Eclipse into your Applications folder, then do
so.
* If you run Linux:
You might get a .tar.gz file a self-extracting .bin file. Extract the
.tar.gz file to your favorite directory, or execute the self-extracting
.bin file.
Configuring Eclipse
Your Eclipse installation might need a bit of tuning. Here's what you do:
1. Launch Eclipse.
© 2011 John Wiley & Sons, Inc.
8
When you launch Eclipse, you see a Workspace Launcher dialog. The
dialog asks where, on your computer's file system, you w ant to store the
code that you will create using Eclipse.
2. In the Workspace Launcher dialog, click OK to accept the default.
Or don't accept the default! One way or another, it's no big deal.
If this is your first time using a particular Eclipse workspace, then
Eclipse starts up with a Welcome screen. Through the ages, most of the
Eclipse Welcome screens have displayed a few icons along with very
little text.
3. Hover over the icons on Eclipse's Welcome screen until you find an
icon whose tool tip contains the word Workbench.
4. Click the Workbench icon to open Eclipse's main screen.
A view of the main screen, after opening Eclipse with a brand new
workspace, is shown in Figure 1.
© 2011 John Wiley & Sons, Inc.
9
Figure 1: The Eclipse workbench with a brand new workspace.
Eclipse is running. Now the fun begins.
© 2011 John Wiley & Sons, Inc.