Appendix 2:

Some Notes on Java Programming Environments


ANYONE WHO IS LEARNING to program has to choose a programming environment that makes it possible to create and to run programs. Programming environments can be divided into two very different types: integrated development environments and command-line environments. All programming environments for Java require some text editing capability, a Java compiler, and a way to run applets and stand-alone applications. An integrated development environment, or IDE, is a graphical user interface program that integrates all these aspects of programming and probably others (such as a debugger, a visual interface builder, and project management). A command-line environment is just a collection of commands that can be typed in to edit files, compile source code, and run programs.

I have programmed using both IDEs and command-line environments, and I have taught programming using both types of environments. Based on my experience, I recommend a command line environment for beginning programmers. IDEs can simplify the management of large numbers of files in a complex project, but they are themselves complex programs that add another level of complications to the already difficult task of learning the fundamentals of programming. Certainly, a serious programmer should have some experience with IDEs, but I think that it's an experience that can be picked up later. This is, of course, just my opinion.

In the rest of this appendix, I'll make a few comments on programming environments. No matter which type of environment you prefer, there is no need to pay for it, so I'll limit my comments to software that is available at no charge. Please note that I am not an expert on Java programming environments. I am including this appendix because people occasionally write to me for help or advice on the matter. In general, however, I cannot answer questions about specific programming environments.


The Basics from Sun (and Apple)

Java was developed at Sun Microsystems, Inc., and the primary source for information about Java is Sun's Java Web site, http://java.sun.com/. At this site, you can read documentation on-line and you can download documentation and software. You should find some obvious links on the main page. (As of July 1, 2002, they are labeled "Download Now," and a page with various downloads can be found at http://java.sun.com/j2se/1.4/download.html.)

The documentation includes the Java API reference and the Java tutorial. These are not really directed at beginning programmers, but you will need them if you are going to be serious about Java programming.

As I write this, the current version of Java on the Sun site is version 1.4. It is available for the Windows, Linux, and Solaris operating systems. You want to download the "J2SE 1.4 SDK." This is the "Java 2 Platform Standard Edition Version 1.4 Software Development Kit." This package includes a Java compiler, a Java virtual machine that can be used to run Java programs, and all the standard Java packages. You want the "SDK", not the "JRE". The JRE is the "Java Runtime Environment." It only includes the parts of the system that are need to run Java programs. It does not have a compiler. You'll also see the "J2EE SDK." This is the "Enterprise Edition," which includes additional packages that are not needed on a personal computer. Don't forget to read and follow the installation instructions.

This textbook is based on Java Version 1.3. If you already have version 1.3, you don't need to download version 1.4 just to use this book.

The Sun site does not have a Java Software Development Kit for Macintosh. However, the Macintosh OS X operating system already includes Java (Version 1.3 as of July 2002). A Java programming environment is available on the Development CD that comes with OS X. Unfortunately, Java 1.3 is not and will never be available for Macintosh OS 9 and earlier. Java 1.1 can be used on older Macintosh systems, and if you are working on one of those, you might want to use the previous edition of this book. Information about Java on Macintosh can be found at http://www.apple.com/java. For Java programming, see http://developer.apple.com/java.


Integrated Development Environments

It is really quite remarkable that there are sophisticated IDEs for Java programming that are available for free. Here are the ones that I know about.

There are other products similar to JCreator, for Windows and for other operating systems, and you might want to look around if you want some of the convenience of an IDE without all the complexity.

If you want to use any of the sample source code from this book in any of these environments, you will have to figure out how to get the code into the environment. In general, IDEs work with "projects". A project contains the all the source code files needed in the project as well as other information. All this is stored in a project directory. To use a source code file from outside the project, you have to "import" it in some way. Usually, you have to copy the file into the project directory or into a source code directory inside the project directory. In addition to this, you have to use an "Add File" command in the IDE to tell it that the file is part of the project. Details vary from one IDE to another. If all else fails, try using a "New File" command to create an empty window in the IDE, and then copy-and-paste the source code from a web browser window into the IDE's window.


Text Editors

If you decide to use a command-line environment for programming, 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:

There are many free text editors that have some or all of these features. Since you are using Java, you should certainly consider jedit, a programmer's text editor written entirely in Java. It requires Java 1.3 or better. It has many features listed above, and there are plug-ins available to add additional features. Since it is written in pure Java, it can be used on any operating system that supports Java 1.3. In addition to being a nice text editor, it shows what can be done with the Swing GUI. Jedit is free and can be downloaded from http://www.jedit.org.

In my own work on Macintosh, I have used BBEdit for Macintosh from Bare Bones Software (http://www.barebones.com/). BBEdit is not free, but there is a free version called BBEdit Lite.

On Linux, I generally use nedit. 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/ and is available for many UNIX platforms in addition to Linux. Features such as syntax coloring and auto-indentation are not turned on by default. You can configure them in the Options menu. Use the "Save Options" command to make the configuration permanent. Of course, as alternatives to nedit, the Gnome and KDE desktops for Linux have their own text editors.

Since I have very little experience with Windows, I don't have a recommendation for a programmer's editor for Windows, other than jedit.


Using the Java SDK

If you have installed Sun's Software Development Kit for Java, you can use the commands "javac", "java", and "appletviewer" for compiling and running Java programs and applets. These commands must be on the "path" where the operating system searches for commands. (See the installation instructions on Sun's Java web site.) The rest of this appendix contains some basic instructions for using these commands with this textbook.

I suggest that you 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 page.

To use the SDK, you will have to work in a command window, using a command-line interface. In Windows, this means a DOS window. In Linux/UNIX, it means an "xterm" or "console" or "terminal" window. Open a command Window and change to the directory that contains your Java source code files. Use the "javac" command for compiling Java source code files. For example, to compile 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 the command 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 Linux and UNIX, a command window usually has a scroll bar that you can use to review the errors. In Windows 2000/NT/XP (but not Windows 95/98), you can save the errors in a file which you can view later in a text editor. The command in Windows is

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. But if you start defining classes in other packages, the source files must be in subdirectories with corresponding names. For example, if a class is in the package named utilities.drawing then the source code file should be in a directory named drawing, which is in a directory named utilities, which is in the top-level program directory. You should work in the top-level directory and compile the source code file with a command such as javac utilities/drawing/sourcefile.java on Linux/UNIX or javac utilities\drawing\sourcefile.java on Windows. If you don't do it like this, the compiler might not be able to find other classes that your class uses.)

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 SDK 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". Classes in other packages should be in subdirectories.)

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 SDK 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 command window. This can be useful for debugging your applet.

You can use the appletviewer command on any file, or even on a web page address. It will find all the applet tags in the file, and will open a window for each applet. If you are using a Web browser that does not support Java 2, you could use appletviewer to see the applets in this book. For example, to see the applets in Section 6.1, use the command

appletviewer http://math.hws.edu/javanotes/c6/s1.html

to view the applets directly off the web. Or, if you have downloaded the textbook, you can change to the directory c6 and use the command appletviewer s1.html to see the applets.

Of course, it's also possible to view your own applets in a Web browser. Just open the html file that contains the applet tag for your applet. One problem with this is that if you make changes to the applet, you might 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 loaded.


[ Main Index ]