A Simple Java Programming Environment

The Edit-Compile-Execute Cycle

When you are working on a program in any object-oriented language, you are typically working with several files, each containing code for a class. You do not write all of the code at once, compile it, run it, and expect to be done. Instead, you add a little bit of functionality at a time. For each increment of functionality, you usually need to modify several of the class files, compile them, then run the modified program to check the correctness of the new functionality. Even if you are an experienced programmer, you should expect to make some mistakes, so you may need to go back and modify some of the files, recompile them, and rerun the program. For this kind of work, it is important to have program development tools set up to simplify the edit-compile-execute cycle.

I suggest that you use the emacs editor for creating and modifying program files. It is set up so that it runs in its own window, independent of other development software that you are using. This allows you to give UNIX commands without terminating the editing program.

The tool that you use to execute a Java program depends on whether you are working on an applet or a stand-alone program. The most convenient tool for executing applets during development is the appletviewer program. It also runs in its own window. For stand-alone programs, you need to use the java program. It is executed by giving a command in the terminal window, described in the next section.

Finally, you will be writing programs on a UNIX machine, so you need a terminal window for giving UNIX commands. This is the window that is used to compile your programs using the javac compiler. If you are developing a stand-alone application, you use the terminal window to start up the Java virtual machine to execute your program.

When you are working from a Windows machine, such as the machines in the MWAH 177 lab, the terminal window is the one that you open up with X-Win32. On UMD Windows machines, you can start up an X-Win32 terminal to our "ub" UNIX machine by doing the following.

On UNIX machines, such as the ones in the Computer Science Lab (HH 314), you can open up a terminal window directly.

Most of the time you have at least two windows for doing your work.

If you are developing an applet you also have In addition, you may also have a browser window open for reading instructions for a lab exercise or programming assignment. The use of the terminal, emacs, and appletviewer windows is described in more detail in the following sections.

It is helpful to set up separate directories for each of the exercises and assignments that you work on. If you do this, it will be easier to find just the files that you need to work on for your current exercise or assignment. The following sections assume that you are doing this.

The Terminal Window

This window is used for all UNIX commands. You should keep in mind that UNIX and all of the UNIX programs described in this web page are case sensitive. The file names "helloworld.java" and "HelloWorld.java" name two distinct files. Also keep in mind that the execution of a UNIX command does not begin until you have terminated the command by hitting the "Enter" key.

The commands that you use depend on how far along you are in developing your program.

Starting an Exercise or Assignment

At this point, you need to create a new directory, move into it, and start up an editor.

Starting a Work Session After the First

When you are starting a new work session after the first, you will normally use the same commands as in the first session except for the mkdir command. You only need to create the directory the first time.

To Compile Your Program

After some editing, you need to compile the modified files.

The First Execution of an Applet in a Work Session

Executing a Stand-Alone Program
Later Work in a Work Session

For further work on an applet in a work session, if you use emacs and appletviewer as described in the following sections then you will only need to give their commands once in a work session. Then further work involves cycles of editing in the emacs window, giving the javac command in the terminal window to compile, and reloading the program in the appletviewer window.

If you are working on a stand-alone Java program then you will need to give the java command each time you want to execute the program. Although it will create a new window if your program has a graphical user interface, there is no mechanism for reloading classes that have been recompiled.

More About UNIX

Here are some more UNIX file commands that you may use from time to time.

Here are some UNIX directory commands that you may occasionally use.

UNIX maintains a list of the 20 most recent commands. You can re-execute an earlier command with just an exclamation point (!) followed imediately by enough of the command to distinguish it from other commands. For example, if you earlier recompiled everything with the command

    javac *.java
Then you can do it again with the command

The emacs Window

If you have started up the emacs program with "emacs &" then you will not need to shut it down to give other commands in the terminal window. You can just keep the emacs window open for the duration of a work session.

The first time you edit a file you will need to open it with the "Open File" menu item on the "Files" menu. The name is entered on the bottom line of the emacs window. It will be added to the emacs buffer list. You can then re-edit it by selecting it from the "Buffers" menu. Be sure to save your work on one file before recompiling or moving on to another file. This is done with the "Save Buffer" menu item on the "Files" menu.

If you have already created one or more .java files and you are starting a new work session, you can enter all of your .java files into the emacs buffer list with the "Open File" menu item on the "File" menu. Just enter *.java for the file name.

If you have never used emacs before, you will probably want to get a .emacs configuration file from someone. I have one that is publicly readable. It was obtained from Tim Colburn, our local emacs guru. You can copy it to your own directory by giving the following command in a terminal window.

    cp ~gshute/.emacs ~
This file is read by emacs each time that it is started up. It will configure emacs to do automatic indentation, which is quite important for editing programs.

The appletviewer Window

If you have started up the appletviewer program with "appletviewer &" then you will not need to shut it down to do other work. After you have modified your program and recompiled it, you can have appletviewer reload the revised program by selecting "Reload" from the "Applet" menu.

Be sure that you have saved files and recompiled them before reloading the program. If you have made changes in a program but do not get any expected changes in its behavior in appletviewer, it may be due to failure to save the buffer or recompile. Even if you think that you saved changes and recompiled, do it again to make sure.