|
Using the JDK
This Web page will help you explore the "mechanics"
of Java programming. You'll learn how to:
-
Download and install the JDK from Sun
-
How to set up your Java programming environment
-
How to compile, edit, and run your programs
The examples here all asume you are running Windows95/98, NT, ME, or W2K.
The process for downloading and installing the IBM version of the JDK is
almost identical.
|
For Macintosh Users
|
| If you are programming on a Mac, you'll need to download
both the Macintosh Runtime for Java [MRJ 2.1.2] as well as the MRJ SDK.
Once you've installed them, just follow the directions. [One hint that
may help you: the Mac tools--javac, etc.--are supposed to be all drag'n'drop;
rather than typing in commands like javac, you simply edit your source
code and then drop it on the javac icon. In fact, I've had some difficulty
getting this to work. Instead, I've found that I need to create a folder
and put both my source code and a copy of the javac compiler into the folder.]
I'm always on the lookout for instructions and screenshots if you
work in the Mac environment. |
Download and
Install the JDK
There are several versions of the Sun JDK.
These are numbered in major and minor versions. The major versions are:
-
Java 1.0: This is the original version of Java and is widely supported
in all Web browsers that run Java. Java 1.0 had about 2,000 different methods
in about 200 different classes.
-
Java 1.1: The first major revision of Java. The major change was
a different "event model". [Simply a different way of handling interaction
with the user]. Java 1.1 is supported on IE4 and above as well as Navigator
4.06 and above for Unix and Windows. Java 1.1 is not supported on Navigator
for the Mac; there you must use IE. Java 1.1 has a little more than 5,000
different methods in about 500 classes.
-
Java 1.3 [Also called the Java 2 Platform]. This is the latest
release of Java, and it doesn't run on any browsers [not
even Sun's all-Java browser, Hot Java]. Instead, Sun provides the "Java
Plugin" if you want to run Java applets. Java 2 contains about 20,000
methods in about 1,500 classes.
To complete this class, you must use, at least, Java 1.1. You can download
Java 2 if you like, but we will not be making use of the new Java 2 features.
[You can write Java 1.0 and 1.1 code using both Java 1.1 and Java 1.2 JDKs]
There are also minor versions of Java 1.1. These versions are numbered
with a third digit like this:1.1.2, 1.1.3,...1.1.8. Your textbook has version
1.1.2 on CD-ROM. This is revision 2 of JDK 1.1. You can use that to complete
all of the exercises in the book, but I'd suggest you download the latest
version of the Java 1.1 JDK, which is 1.1.8. The minor version updates
of the Java 1.1 are bug fixes; they don't add any new features.
Step 1: Locate the JDK
For Windows and Solaris, you can download the JDK directly from Sun Microsystems
for free. The link for the Windows version of the JDK 1.1.8 is at:
http://www.javasoft.com/products/jdk/1.1/download-jdk-windows.html
You can download the Java 2 Platform, Standard Edition Software Development
Kit (J2SESDK) link for free as well. That link, for the Windows version
of SDK 1.3, is:
http://www.javasoft.com/j2se/1.3/download-windows.html
If you're undecided about which one to download, the size might make a
difference. JDK 1.8 is about 9MB, while the Java 2 download is around 30MB.
Step 2: Select Your Download
Once you've arrived at the Sun site, scroll on down the page until you
locate the download area. For JDK 1.1, it will look like this:
If you are using a slow or otherwise unreliable connection, you may want
to download the JDK as small pieces, each of which will fit on a 1.44 floppy
disk. Make sure you carefully follow the instructions for reassembling
them if you do so.
If you elect to download the Java 2 SDK, you will have to scroll past
a whole bunch of advertisements for various IDEs [Integrated Development
Environments] before you get to the actual SDK download area at the bottom
of the page. The Java 2 SDK download area looks like this:
When you click on the Continue button, you'll be asked to agree to the
usual inscrutable Web licensing agreement. [You'll also be asked to agree
once again before you install the JDK]. Since you can't download or install
without agreeing, you may as well go ahead.
Step 3: Download and Save
Once you agree, you'll be shown to a screen where you can begin the final
download, shown below.
Note the name and size of the download file. Write down this information
so you can compare it to what actually is downloaded to your machine. If
the size of the downloaded file is different that what is shown, then a
problem occured during download and you'll have to try again. If the name
is different [some browsers will rename downloaded files] you'll simply
have to rename the file after it's downloaded.
Once you've finished, then click on either the FTP or HTTP download buttons.
[FTP is faster, but the Sun FTP servers seem to be a little busier. If
your browser can't seem to connect to the FTP server, then try the HTTP].
Select a location for the downloaded file somewhere on your local hard
disk, and sit back and enjoy a cup of coffee while it downloads. Make sure
you remember where you downloaded the file, however.
Afte the file is downloaded, you're ready to begin the install.
Step 4: Install the JDK
After what you've gone through up to this point, installation itself is
rather anticlimatic. Make sure you have enough free disk space [about 20
MB for JDK 1.1.8, 80 MB for Java 2 SDK], locate the file you downloaded
in Step 3 using Windows Explorer, and double-click it.
This will start up a pretty standard installation program. You don't
really have much to decide except for the screen shown below, where you
can:
-
Install to a different path. The default is C:\jdk1.1.8 for the Java 1.1.8
version. If you're going to be installing newer versions as time goes by,
then just accept the default. If you think you'll never want more than
one version of the JDK on your system at any time, then you may want to
shorten this to C:\jdk.
-
Decide which pieces to install. Normally, the default installation, shown
here, is a good choice. If you want to look at the source code for the
built-in Java packages, you can install the Java sources which takes another
5 MB.
Other Downloads
Once the installation is complete, you'll be given an opportunity to view
the "Read-Me" file, which you should do. [You may want to print it out
as well.] You don't really need to use the configuration instructions,
however, because we'll be configuring the JDK using a slightly different
technique shortly.
Before we go on, you may want to download and install the Java documentation.
If you normally work online, as I do, this isn't really necessary; but,
if you want a local copy, you should go ahead. Go to the same site where
you acquired the JDK and look for the "documentation bundle". Once you've
downloaded it, install it in the same directory where you've installed
the JDK.
At this point, you're ready to go on to the next step, which is configuring
your environment.
Your Programming
Environment
 |
If you're an old hand at working with command-line
tools in an environment like DOS or Unix, then you probably won't have
any problem starting up a command prompt, locating the JDK tools, and running
the javac compiler or the appletviewer.
Even for old DOS hands, however, creating a desktop shortcut like that
shown here, can free your mind for important things, like writing Java
programs. |
Here are the steps you need to follow. Read through them first, and
then we'll walk you through the process, step-by-step. The instructions
will vary a little depending upon whether you are using JDK 1.1 or Java
2, and whether you are using Windows 95/98 or Windows NT. If you are using
Windows 2000 or Windows ME, follow the instructions for Windows NT.
-
Step 1: Create a Shortcut. Right-click on your desktop and
choose New|Shortcut from the context menu.
-
Step 2: Enter Program Name. In Windows 95/98, enter COMMAND.COM.
In Windows NT/ME/2K, enter CMD.EXE instead.
-
Step 3: Select a Title. Add the title you want to appear
on the desktop.
-
Step 4: Create a Batch File. Create a batch file in the JDK
directory. Name it StartJDK118.BAT [W95/98, JDK 1.1.8], or StartJDK13.CMD
[NT/2K, Java 2].
-
Step 5: Modify Path and CLASSPATH. Edit batch file to add
path to Java command-line tools, and CLASSPATH information.
-
Step 6: Create a subdirectory to hold your files. Use Windows
Explorer to create a folder to hold your Java files named C:\JavaProjects.
-
Step 7: Adjust the Shortcut Properties. Right-click Java
shortcut, choose Properties from the popup menu. Select the Shortcut panel,
supply directory and path to your batch file.
-
Step 8: Change Your Icon. Click Change Icon and select the
Java coffee-cup icon.
-
Step 9: Test Your Installation. Double-click your new shortcut
and try it out by typing java at the command prompt.
Here are the steps in detail.
Step 1: Create a Shortcut.
Right-click
on your desktop, and choose New|Shortcut from the context-menu that appears,
as you can see here. (In W95, or NT, you won't see the small icons next
to Folder and Shortcut like in W98.)
Step 2: Enter Program Name
When the Create Shortcut dialog box appears, as you can see below, type
cmd
in the text field labeled Command line if you are using NT like this:
If you are using Windows 2000 or Windows ME, the Create Shortcut dialog
will look like this:
If you are using W95/98, then type command instead, as shown below:
Windows automatically knows you want to run the 32-bit command interpreter,
CMD.EXE
in Windows NT or 2K, and the MSDOS command interpreter,
COMMAND.COM
in Windows 95/98. When you're finished, click Next.
Step 3: Select a Title
When the "Select a Title for the Program" dialog box appear, type in the
name you wish to appear underneath the shortcut on your desktop. In the
example shown here, I've just entered Java 1.1, but you may wish to be
more specific.
One of the real advantages of shortcuts is that you can set them up to
run different versions of the JDK. You might have one version for the original
JDK 1.0, another that runs JDK 1.1, and one that runs SDK 1.3 for the Java
2 Platform.
Here's what the "Select a Title" dialog looks like with Windows 2K:
When you've typed in your name, click Finish.
Step 4: Create a Batch File
The instructions that come with the JDK (in the ReadMe file) require you
to modify your AUTOEXEC.BAT file or to change the SYSTEM environment. I
find it easier to create a batch file to run the JDK so that I can easily
switch between different versions.
To create your batch file, use Windows Explorer to locate your JDK directory.
In the example shown here, it is C:\jdk1.3, but if you've installed
JDK 1.1, it will probably be C:\jdk1.1.8. Before you actually
create your batch file, make sure that the Windows Explorer Folder Options,
[accessible from the Windows Explorer Tools menu], are set to "Show all
files" and that file extensions are not hidden, like this: (This dialog
may look different on your machine.)
Once you have opened the JDK folder, choose File|New|Text Document
from the main Explorer menu. Windows will create a new file in your JDK
directory named "New Text Document.txt". Rename the file like this:
-
For JDK 1.1.8, name the file StartJDK118
-
For J2 SDK 1.3, name the file StartJDK2
-
For Windows 95/98, use the extension .BAT
-
For Windows NT, use the extension .CMD
Windows will complain at you when you attempt to change the file extension,
like this:
Ignore the warning [by clicking the "Yes" button]. At this point, you should
see something that looks like this. [The example shown is for Windows 2K
and Java 2. Your display may look different. Make sure, however, that the
file Icon is a small gear, and not the default Text file Icon.]
Once you've created your batch file, simply right-click it and choose Edit
(not Open) from the popup menu that appears. Now you're ready for
the next step.
Step 5: Modify Your Path and
Classpath
When you edit you batch file, Windows will open up Notepad. You'll need
to add lines to modify the PATH, the CLASSPATH, and, in the case of Windows
95/98, run the DOSKEY utility as well.
Windows NT/2K and Java 2
Here is what your file should look like for Windows NT and Java 2:
As you can see, the file contains two lines:
PATH=C:\jdk1.3\bin;%PATH%
SET CLASSPATH=.;C:\jdk1.3\jre\lib\rt.jar
The first line places the Java 2 command-line tools directory in front
of any existing PATH that may exist. The second line gives your programs
access to the Java 2 runtime library, contained in the file rt.jar.
Windows 95/98 and JDK 1.1
Here is what your file should look like for Windows 95/98 and Java 1.1:
As with the Windows NT version, this also contains two lines:
PATH=C:\jdk1.1.8\bin;%PATH%
SET CLASSPATH=.;C:\jdk1.1.8\lib\classes.zip
The biggest difference between the two is that the Java runtime library
is contained in a file named classes.zip, located in the C:\jdk\lib
directory. Under Java 2, the runtime library is located in the file rt.jar,
located in the C:\jdk\jre\lib directory.
If you are using Windows 95/98 [but not NT] you'll also want to add
one more line to your batch file:
DOSKEY /INSERT
The side-bar explains why adding DOSKEY is a good idea. In Windows NT/2K,
the functionality of DOSKEY is built into the 32-bit command interpreter,
CMD.EXE.
|
What is DOSKEY?
|
| Because you will be repeating the Edit-Compile-Test cycle more times
than you'd like, DOSKEY allows you to repeat a previous command-line incantation
simply by using the arrow keys to recycle them. Pressing up-arrow
brings back the previous commands, one-by-one. DOSKEY also allows you to
edit your commands before pressing ENTER. |
You may also want to start up your programmer's text editor, whenever
you start the JDK. The batch file is a great place to do that. For instance,
to start the Scintilla Text Editor whenever you launch your JDK:
-
Download the executable file Sc1.exe
and save it in your JDK bin directory.(That's the directory that contains
the file JAVA.EXE.)
-
Add one more line to the end of the batch file containing the single word:
SC1
Now, when you launch your JDK, the SciTE text editor will automatically
start up, and you'll be able to compile your Java programs from within
the editor.
Step 6: Create a Working Directory
Create
a folder to save your Java programs.
Using Windows Explorer, select New|Folder from the main menu,
and make a directory called C:\JavaProjects, [or some other name if you
prefer], as you can see here:
Step 7: Setup the Shortcut Environment
When
Windows creates the shortcut for you, as you can see in the figure here,
it automatically uses the MS-DOS icon, and also uses your default MS-DOS
environment. We want to change both of those.
Right-click on your new shortcut and you should see a context menu similar
to that shown here. If, instead, you see another menu, make sure that the
shortcut icon is highlighted, as it appears in the figure. From the context
menu, choose Properties.
Windows 95/98 and JDK 1.1
Windows 95/98 and Windows NT/2K have different shortcut property sheets.
We'll look at Windows 95/98 first. Choose the second tab, titled "Program"
and change the fields as shown here:
Change the text in the "Working" text field so that it refers to the
directory you created in Step 6. Change the value in the "Batch file" text
field to:
C:\jdk1.1.8\StartJDK118.BAT
(As before, this assumes you have installed the JDK into the C:\jdk1.1.8
directory. If you've installed it somewhere else, or if you are using Java
2, change this to reflect the actual name and location of the batch file
you've created.)
Windows NT or 2K and Java 2
In Windows NT, the second tab is titled "Shortcut" instead of "Program".
Note that there is no field to enter a batch file name. Instead, replace
the contents of the Target field with:
\system32\CMD.EXE /K C:\jdk1.3\StartJDK13.CMD
There are three parts to this:
| \system32\CMD.EXE |
run the 32-bit command interpreter |
| /K |
remain in memory |
| C:\jdk1.3\StartJDK13.CMD |
the batch file to start with |
In the "Start in" text field, add C:\JavaProjects, just as you did with
95/98. The finished Java Properties dialog should look like this:
Windows 2000 and Windows ME are very similar to the Windows NT procedures,
but the Dialog box looks slightly different, as you can see:
Before we get around to testing your installation, let's take care of some
more pressing needs. Let's fix the icon so that you can tell you're working
with Java! Do that by clicking Change Icon button on the property sheet,
and then going to the next step.
[In Windows NT, this may bring up a misleading warning, similar to that
shown here. You can safely ignore this.]
Step 8: Change Your Icon
When you click Change Icon, Windows shows you a list of icons that it has
available. You aren't limited to the icons that Windows supplies, however.
Instead, click Browse, and navigate to the jdk1.1.8\bin directory
(as shown in the figure here), and select the file named winawt.dll:
For Java 2, navigate to the C:\jdk1.3\jre\bin directory as
shown here, and locate the file named awt.dll.
If you cannot find either file, make sure that Windows is showing you
all files by changing the Folder Options to "Show all files" as mentioned
earlier.
Step 9: Test Your Installation
Double-click your new shortcut and you should see a command window similar
to that shown in the first three lines below. At the command prompt, type
javac
and press ENTER. If you've set up everything correctly, you'll see a list
of the JAVAC.EXE compiler options as shown in the bottom of your
screen.
If, instead, you see an error message that says "Bad Command or File Name",
that means you've misstyped the path to the Java executable. Reedit your
batch file, and try again.
Using the JDK
Learning is a process that involves reading,
and understanding, and doing as well. In programming, there's
nothing quite so helpful as trying out an example to make sure you really
understand. The easiest way to learn to use the JDK is to create both a
Java applet and an application, using the JDK development tools.
Creating Your Source Code
Now that the JDK is configured, let's use it to create your source-code
files. Double-click your new shortcut, type EDIT, under Windows
95/98, or Notepad, under Windows NT, and then type and save each
of the programs listed below. (If you installed the SciTE text editor,
you don't need to start up EDIT or Notepad.)
-
Put each listing into a separate file. Do not put them together into one
file.
-
Make sure you name each file correctly: FirstApplet.java, FirstApp.java,
and FJA.html. The case of each filename is significant. [If you
are using Notepad, make sure you don't inadvertently end up with files
named FirstApplet.java.txt. If that happens, place the entire filename
inside quotes when saving the file.]
-
Make sure you carefully check the spelling of each word in each file. Like
the filenames, the case of each word is significant in a Java program.
The words main and Main, for instance, are two different
things.
-
Save all three files in your JavaProjects directory.
Listing 1: FirstApplet.java
|
|
import java.awt.*;
import java.applet.*;
public class FirstApplet extends Applet
{
Label theLabel;
public void init()
{
theLabel = new Label("Hi Mom! Your
child, Applet");
add(theLabel);
}
} |
Listing 2: FJA.html
|
|
<html><head><title>First Applet</title></head>
<body>
<applet code=FirstApplet width=300 height=200>
</applet>
</body>
</html> |
Listing 3: FirstApp.java
|
|
public class FirstApp
{
public static void main(String[] args)
{
System.out.println("Hi Mom! -
Your child, App");
}
} |
Compiling Your Code
Once you've created your source-code files, the next step is to compile
your programs, producing Java byte-code. To compile your programs, return
to the command prompt and type in the following:
C:>\JavaProjects\> javac FirstApp.java
C:>\JavaProjects\> javac FirstApplet.java
If there are no problems, you will simply be returned to the command prompt.
Following are some problems you may encounter, along with their solutions:
Misspelling the name of the source code file. If you ask the compiler to
compile FistApplet.java instead of FirstApplet.java, you'll
see a message like this:
C:\JavaProjects\> javac FistApplet.java
error: Can't read: FistApplet.java
1 error
-
Being careless with your capitalization or spelling when entering your
source code. If, inside your source code, you name your class FIRSTAPPLET
instead of FirstApplet, you'll see a message like this:
C:\JavaProjects\> javac FirstApplet.java
FirstApplet.java:4: Public class FIRSTAPPLET must be defined in
a file called "FIRSTAPPLET.java".
public class FIRSTAPPLET extends JApplet
^
1 error
-
Adding or forgetting punctuation. Remember, your very literal computer
only pretends to be intelligent. It gets completely lost when you
fail to spell everything out exactly. For instance, if you inadvertently
use a parentheses ( instead of a brace { when typing in your listing, the
compiler will tell you this:
C:\JavaProjects\> javac FirstApplet.java
FirstApplet.java:9: '{' expected.
public void init()
^
1 error
Notice that the compiler does its best to point to your the program and
line number where it gets lost. As you learn more about Java's syntax,
you'll find these messages more helpful.
Run Your Code
Once you've successfully compiled your programs, it's time to try them
out. Follow these instructions:
In your DOS command window, run your applet by typing:
C:\JavaProjects\> appletviewer FJA.html
This should start the appletviewer application, which will launch
a separate window, and display your message inside it.Here's what the FirstApplet
Java applet looks like as it runs:
Close the appletviewer window, using either the closing control
in the upper-right corner of the appletviewer window, or by simply pressing
<CTRL>-<C> from the DOS window that launched the appletviewer.
Return to the DOS command window and run your application by typing:
C:\JavaProjects\> java FirstApp
The Java interpreter should run your program and display its output in
the same window.
Here's what your the FirstApp Java application looks like as
it runs:
Materials
on this web site © 1995-2001, Stephen
Gilbert. All rights reserved.
Any materials may be reproduced
for non-profit purposes so long as
(1) this notice remains intact
and (2) you notify me of your use.
|