Appendix 2:

Some Notes on Java Programming Environments

EACH TIME I HAVE TAUGHT a course based on this textbook,
I have used a different programming environment: CodeWarrior for Macintosh,
Visual J++ for Windows, and the JDK for Linux. Of the three development
environment environments, the JDK was by far the most successful. CodeWarrior
and Visual J++ are examples of integrated development
environments. That is, they include a text editor, an interactive debugger,
a compiler, and tools for managing large programming projects. The problem with
such environments in an introductory course is their complexity, which can
be overwhelming for a beginner who is having trouble enough learning the basics
of programming. The JDK (Java Development Kit) uses a command-line
interface, in which the user types commands to compile and run Java programs.
Although this interface is a bit alien to students who are used to working
with a graphical user interface, I have not run into any students who had
difficulty understanding or using it. Another advantage of the JDK is that
it is free.

In this appendix, I'll give some information and opinion on Java programming
environments for Windows, Macintosh, and Linux/UNIX. I'll concentrate on free software,
including the JDK. There is a lot of redundancy among the sections on Windows,
Macintosh, and Linux/UNIX. You probably only need to read one of these sections.

But first, let me describe the computing setup that I use in my
courses, since that might be of interest to other professors.

My department has a small computing lab of its own,
with a mixture of Linux, Windows, and Macintosh computers. The majority of the computers
run Linux. The Linux computers use a graphical ("XDM") login screen and the KDE
desktop, which means that they work much the same as Windows or Macintosh from the user's
point of view. The lab is not large enough for introductory programming classes, but
students can log on to the Linux machines in the lab from any Windows computer on campus.
For this, we use X-Win32, from StarNet (www.starnet.com).
Using X-Win32, students see
the same login screen and desktop as they see when they work on the machines directly in the lab.
(Although X-Win32 is fairly expensive for commercial use, StarNet has a very reasonable academic
pricing policy.) Student home directories are on a deparmental server, and are accessible
from anywhere on campus. The Linux distribution that we use, currently SuSE 6.1, provides many
tools that we use in introductory programming and other courses, including the JDK, text editors, C++,
Lisp, Prolog, and OpenGL (in the form of Mesa).

Sources of Software and Information

If you work in Windows, I suggest that you take a look at burks.brighton.ac.uk.
You'll find a large collection of Windows software that is useful for students of computer science. The collection
can be purchased on a set of cheap CD's, and it is available on-line at
http://burks.brighton.ac.uk/burks/index.htm.
(Version 4 of this collection includes the second edition of this textbook. It also has a copy of
JDK 1.1.6, which is suitable for use with this textbook.)

Text Editors

Before you start writing programs, make sure that you have a good text
editor. A programmer's text editor is a very different thing from a word
processor. Most important, it saves your work in plain text files and
it doesn't insert extra carriage returns beyond the ones you actually type.
A good programmer's text editor will do a lot more than this. Here are some
features to look for:

Syntax coloring. Shows comments, strings, keywords, etc., in different colors
to make the program easier to read and to help you find certain kinds of errors.

Function menu. A pop-up menu that lists the functions in your source code.
Selecting a function from this will take you directly to that function in the code.

Autoindentation. When you indent one line, the editor will indent
following lines to match, since that's what you want more often than not
when you are typing a program.

Parenthesis matching. When you type a closing parenthesis the cursor jumps back to
the matching parenthesis momentarily so you can see where it is. Alternatively,
there might be a command that will hilite all the text between matching parentheses.
The same thing works for brackets and braces.

Indent Block and Unindent Block commands. These commands apply to a hilited block
of text. They will insert or remove spaces at the beginning of each line to increase or
decrease the indentation level of that block of text. When you make changes in your
program, these commands can help you keep the indentation in line with the structure
of the program.

Control of tabs. My advice is, don't use tab characters for indentation. A good
editor can be configured to insert multiple space characters when you press the tab key.

There are many free text editors that have some or all of these features.

For Linux and UNIX, I recommend nedit as a nice editor and one that will be
comfortable for people who are used to GUI programs for Windows and Macintosh.
It has all the above features, except a function menu. If you are using Linux,
it is likely that nedit is included in your distribution, although it may
not have been installed by default. It can be downloaded from
http://www.nedit.org/ for many UNIX platforms.
Features such as syntax coloring and autoindentation are not turned on by default.
You can configure them in the Options menu. Use the "Save Options" command
to make the configuration permanent.

For Windows, you might consider the free text editor SynEdit. It can be downloaded at
www.mkidesign.com. This program is distributed
as a "zip" file, so you will need a program such as WinZip to extract it.
(You can download WinZip from www.winzip.com.) I have also used
Editeur, but it is shareware rather than freeware. (However, I haven't used Windows enough
to have a strong recommendation.)

Java for Windows 95 or NT (or later)

If you use the JDK on Windows 95/98, you will be working in DOS command
windows, with a command-line interface. A utility called "doskey", which
is included in the standard Windows installation, will make your life
easier. To install it, add the line

doskey

to your autoexec.bat file. The change will take effect when you
restart your computer. Once doskey is installed, when you are working in a DOS command window,
you can use the
up arrow key to go back to previous commands that you have typed in
that window. Once the command is retrieved, you can edit it, if you want.
Then press return to issue the command again. Since you will often
find yourself issuing the same commands over and over as you compile
and run your programs, this can save you a lot of typing.

Note: To edit your autoexec.bat file, select the "Run" command
from the "Start" menu. Enter the command sysedit
in the dialog that pops up, and press return.
This will open a window where you can edit system configuration files,
including autoexec.bat. Make your change, such as adding the "doskey"
command to this file, and save your changes.

You can download version 1.1 of the JDK for Windows at
http://java.sun.com/products/jdk/1.1/.
You might also want to pick up a copy of the documentation there. JDK 1.1.6 is also available
on the Burks site (http://burks.brighton.ac.uk/burks/index.htm).
You should be able to use a later version of the JDK with this textbook, but not an earlier one.
Follow the installation instructions. This will include modifying your PATH to include the
directory that contains the java compiler and interpreter. (On Windows 95/98, you will have to edit your
autoexec.bat file and restart your computer to do this. See the above note about editing
autoexec.bat. You have to add the path for the bin directory of the java installation
to the end of the line that starts "SET PATH=". If there is no such line in
the autoexec.bat file, add one. The path to the bin directory
will be something like C:\jdk1.1.6\bin if you used the default JDK installation.
The line in the autoexec.bat file will have the form:
"SET PATH=%PATH%;maybe-other-paths;C:jdk.1.6\bin".)

Make a directory
to hold your Java programs. (You might want to have a different subdirectory for each program that you
write.) Create your program with a text editor, or copy the program you want to compile into your
program directory. If the program needs any extra files, don't forget to get them as well. For example,
most of the programs in the early chapters of this textbook require the file TextIO.java. You should
copy this file into the same directory with the main program file that uses it. (Actually, you only need
the compiled file, TextIO.class, to be in the same directory as your program. So, once you have
compiled TextIO.java, you can just copy the class file to any directories where you need it.)

If you have downloaded a copy of this textbook, you can simply copy the files you need from the source
directory that is part of the download. If you haven't downloaded the textbook, you can open the source file in a Web browser
and the use the Web browser's "Save" command to save a copy of the file. Another way to get Java source code
off a Web browser page is to hilite the code on the page, use the browser's "Copy" command to place the code
on the Clipboard, and then "Paste" the code into your text editor. You can use this last method when you want
to get a segment of code out of the middle of a Web browser page.

To use the JDK, you will have to work in a DOS window, using a command-line interface. Open
a DOS Window and change to the directory that contains your Java source code files.
(Tip: In Windows 95/98, open a directory window for the directory, then select the "Run" command from
the "Start" menu, enter "command" in the dialog window that pops up, and press return. A DOS window
will open that is all set for working in the directory shown in the directory window.)

The "javac" command is used for compiling Java source code files. For example,
to compile the Java source code file named SourceFile.java, use the command

javac SourceFile.java

You must be working in the directory that contains the file. If the source code file does not contain
any syntax errors, this command will produce one or more compiled class files. If the compiler finds any
syntax errors, it will list them.
Note that not every message from the javac compiler is an error. In some cases, it generates
"warnings" that will not stop it from compiling the program.
If the compiler finds errors in the program, you can edit the source code file and try to compile it again.
Note that you can keep the source code file open in a text editor in one window while you compile the program
in a DOS window. Then, it's easy to go back to the editor to edit the file. However, when you do this,
don't forget to save the modifications that you make to the file before you try to compile it again!
(Some text editors can be configured to issue the compiler command for you, so you don't even have
to leave the text editor to run the compiler.)

If your program contains more than a few errors, most of them will scroll out of the window before
you see them. In Windows NT only, but not Windows 95/98, yoiu can
save the errors in a file which you can view later in a text editor.
Use the command such as

javac SourceFile.java >& errors.txt

The ">& errors.txt" redirects the output from the compiler to the
file, instead of to the DOS window. For Windows 95/98 I've written
a little Java program that will let you do much the same thing. See the source
code for that program, cef.java, for instructions.

It is possible to compile all the Java files in a directory at one time. Use the command
"javac *.java".

(By the way, all these compilation commands only work if the classes you are compiling are in
the "default package". This means that they will work for any example from this textbook.)

Once you have your compiled class files, you are ready to run your application or applet.
If you are running a stand-alone application -- one that has a main() routine -- you
can use the "java" command from the JDK to run the application. If the class file that contains
the main() routine is named Main.class, then you can run the program with the
command:

java Main

Note that this command uses the name of the class, "Main", not the full name of the class
file, "Main.class". This command assumes that the file "Main.class" is in the current directory, and
that any other class files used by the main program are also in that directory. You do
not need the Java source code files to run the program, only the compiled class files.
(Again, all this assumes that the classes you are working with are in the "default package".)

If your program is an applet, then you need an HTML file to run it. See Section 6.2 for
information about how to write an HTML file that includes an applet. As an example, the following code
could be used in an HTML file to run the applet "MyApplet.class":

<applet code="MyApplet.class" width=300 height=200>
</applet>

The "appletviewer" command from the JDK can then be used to view the applet.
If the file name is test.html, use the command

appletviewer test.html

This will only show the applet. It will ignore any text or images in the HTML file. In fact, all
you really need in the HTML file is a single applet tag, like the example shown above. The applet will
be run in a resizable window, but you should remember that many of the applet examples in this
textbook assume that the applet will not be resized. Note also that your applet can use standard output,
System.out, to write messages to the DOS window. This can be useful for
debugging your applet.

Of course, it's also possible to open the HTML file in a Web browser, such as Netscape or
Internet Explorer. One problem with this is that if you make changes to the applet, you have
to actually quit the browser and restart it in order to get the changes to take effect.
The browser's Reload command might not cause the modified applet to be reloaded.

Java for Macintosh

The Apple Computer company has a free Software Development Kit (SDK) for Java. This SDK includes
Apple's versions of the compiler from the JDK. To use the SDK, you also need to have
a copy of the MRJ (Macintosh Runtime for Java) installed on your computer. The MRJ includes a
Java interpreter for running Java applications and applets. If you have a recent version of the
MacOS, the MRJ might already be installed. If not, you can download it from Apple's Java Web site
at http://www.apple.com/java/. The SDK can be downloaded from the FTP
directory at

You'll need access to three programs from the MRJ and SDK. I suggest that you make aliases to these
programs and leave the aliases on your desktop. (To make an alias, click once on the program icon to hilite
it. Choose the "Make Alias" command from the file menu. This will create an icon for the alias. Drag this
icon onto your desktop.) The programs you need are:

Apple Applet Runner. You will find this in the "Mac OS Runtime For Java" folder inside a sub-folder
named "Apple Applet Runner".

javac. This is part of the SDK. It is in a folder called "JDK Tools" which is
itself inside a folder called "Tools".

JBindary. This is also part of the SDK. You'll find it inside a
folder called "JBindary".

Make a folder
to hold your Java programs. (You might want to have a different folder for each program that you
write.) Create your program with a text editor, or copy the program you want to compile into your
program folder. If the program needs any extra files, don't forget to get them as well. For example,
most of the programs in the early chapters of this textbook require the file TextIO.java. You should
copy this file into the same folder with the main program file that uses it.

If you have downloaded a copy of this textbook, you can simply copy the files you need from the source
folder that is part of the download. If you haven't downloaded the textbook, you can open the source file in a Web browser
and the use the Web browser's "Save" command to save a copy of the file. Another way to get Java source code
off a Web browser page is to hilite the code on the page, use the browser's "Copy" command to place the code
on the Clipboard, and then "Paste" the code into your text editor. You can use this last method when you want
to get a segment of code out of the middle of a Web browser page.

To compile a Java source code file drag the file onto the
javac program icon (or an alias for this program). If the file you are compiling
depends on other source code files, select them all and drag them all onto the javac
icon. For example, if your program uses TextIO, you should drag both TextIO.java and the
source file for the main program onto the javac icon.
(To select multiple files, hold down the Shift key as you click on the second, third, ... files.)
A dialog window will open. It should show the names of the source code files in a box on the upper left.
Click the "Do Javac" button in this window. This will do the compilation. A message window will open with
messages from the compiler. If your program contains any syntax errors, they will be listed in the message
window. You can then edit the file and try to compile it again. Note that you can keep the text editor
window and the javac window open at the same time. After making changes to
the program with the text editor, save your work and hit the "Do Javac" button again.

Once you have the compiled class files, you can run your program. If the program is a stand-alone
application, with a main() routine, you can run it with JBindary.
Drag the compiled class file that contains the main() routine onto the
JBindary icon (or an alias). Even if the main program depends on other
classes, you only have to drag one file onto the icon. (However, the other class files must be
in the same directory with the main class file.) A window should open that shows the name of
your main class in a box at the upper left. This window also has two pop-up menus for "redirecting"
Standard Input and Standard Output. For programs that use TextIO, make sure that both of
these pop-up windows are set to the "Message Window". Click the "Run" button to run your program.
A separate "Message Window" will open for doing standard input and output, if your program uses them.

If your program is an applet, then you need an HTML file to run it. See Section 6.2 for
information about how to write an HTML file that includes an applet. As an example, the following code
could be used in an HTML file to run the applet "MyApplet.class":

<applet code="MyApplet.class" width=300 height=200>
</applet>

To run the applet, drag the HTML file onto the Apple Applet Runner icon (or
an alias).

This will only show the applet. It will ignore any text or images in the HTML file. In fact, all
you really need in the HTML file is a single applet tag, like the example shown above. The applet will
be run in a resizable window, but you should remember that many of the applet examples in this
textbook assume that the applet will not be resized. Note also that your applet can use standard output,
System.out, to write messages. These will appear in a separate "Message Window". This can
be very useful for debugging.

Make a directory
to hold your Java programs. (You might want to have a different subdirectory for each program that you
write.) Create your program with a text editor such as nedit, or copy the program you want to compile into your
program directory. If the program needs any extra files, don't forget to get them as well. For example,
most of the programs in the early chapters of this textbook require the file TextIO.java. You should
copy this file into the same directory with the main program file that uses it. (Actually, you only need
the compiled file, TextIO.class, to be in the same directory as your program. So, once you have
compiled TextIO.java, you can just copy the class file to any directories where you need it.)

If you have downloaded a copy of this textbook, you can simply copy the files you need from the source
directory that is part of the download. If you haven't downloaded the textbook, you can open the source file in a Web browser
and the use the Web browser's "Save" command to save a copy of the file. Another way to get Java source code
off a Web browser page is to hilite the code on the page, use the browser's "Copy" command to place the code
on the Clipboard, and then "Paste" the code into your text editor. You can use this last method when you want
to get a segment of code out of the middle of a Web browser page.

You will need a command-line interface to work with the JDK commands. If you are working with a
graphical user interface, open an xterm (or other command-line window).
Of course, you need the GUI to run Java applets. Change to the directory that
contains your Java source code files.

The "javac" command is used for compiling Java source code files. For example,
to compile the Java source code file named SourceFile.java, use the command

javac SourceFile.java

You must be working in the directory that contains the file. If the source code file does not contain
any syntax errors, this command will produce one or more compiled class files. If the compiler finds any
syntax errors, it will list them. You should have some way of scrolling back to see the first errors in the list.
Note that not every message from the javac compiler is an error. In some cases, it generates
"warnings" that will not stop it from compiling the program.
If the compiler finds errors in the program, you can edit the source code file and try to compile it again.
Note that you can keep the source code file open in a text editor in one window while you compile it
in another. Then, it's easy to go back to the editor to edit the file. However, when you do this,
don't forget to save the modifications that you make to the file before you try to compile it again!

It is possible to compile all the Java files in a directory at one time. Use the command
"javac *.java".

(By the way, all these compilation commands only work if the classes you are compiling are in
the "default package". This means that they will work for any example from this textbook.)

Once you have your compiled class files, you are ready to run your application or applet.
If you are running a stand-alone application -- one that has a main() routine -- you
can use the "java" command from the JDK to run the application. If the class file that contains
the main() routine is named Main.class, then you can run the program with the
command:

java Main

Note that this command uses the name of the class, "Main", not the full name of the class
file, "Main.class". This command assumes that the file "Main.class" is in the current directory, and
that any other class files used by the main program are also in that directory. You do
not need the Java source code files to run the program, only the compiled class files.
(Again, all this assumes that the classes you are working with are in the "default package".)

If your program is an applet, then you need an HTML file to run it. See Section 6.2 for
information about how to write an HTML file that includes an applet. As an example, the following code
could be used in an HTML file to run the applet "MyApplet.class":

<applet code="MyApplet.class" width=300 height=200>
</applet>

The "appletviewer" command from the JDK can be used to view an applet from an HTML file.
If the file name is test.html, use the command

appletviewer test.html

This will only show the applet. It will ignore any text or images in the HTML file. In fact, all
you really need in the HTML file is a single applet tag, like the example shown above. The applet will
be run in a resizable window, but you should remember that many of the applet examples in this
textbook assume that the applet will not be resized. Note also that your applet can use standard output,
System.out, to write messages to the command-line window. This can be useful for
debugging your applet.