Creating Java Programs in Gordon's Macintosh Lab -
Java Development using JJEdit on Macintoshes Running OS X

The purpose of this page is to outline one process students enrolled in CS courses can follow when creating Java programs, whether in lab or for projects. The approach outlined here makes use of a very lightweight integrated development environment (IDE) called JJEdit which we have licensed for use on the Macintoshes in our lab. (Other approaches are to make use of the Unix command line or an "industrial-strength" IDE such as JBuilder. Both of these approaches are introduced in CS211).

From time to time, it becomes necessary to do some fine-tuning of the procedures. Therefore, you should check this page periodically to be sure you have the latest information. (Check the revision date at the bottom to see when this file was last changed).


Logging in to the Computer and Connecting to the Server

Before proceeding with the steps outlined here, it is first necessary to login to the computer and connect to the file server. The steps needed for doing this are outlined on a separate page. (These steps are the same, regardless of what software development tools one is using.)

Please become familiar with this material by following this link before proceeding


Program Creation Steps

Creating a Java program involves three distinct steps:

  1. Creating/editing the source program (creating a .java file).
  2. Compiling the source program into java bytecodes (creating a .class file).
  3. Executing the compiled program (.class file).




JJEdit is a single tool that can perform all of these tasks.

Note that all of the files you will use with this tool will reside on your server volume.

To start JJEdit, you simply need to click its Icon in the dock. Alternately, you can drag an existing java source file to the JJEdit icon and drop it there - this will launch JJEdit prepared to edit that file.




Either method will result in JJEdit's editor window being displayed. In the former case, it will be empty; in the latter, it will contain the file you dragged and dropped on the icon. This is the case that is illustrated below.




The window itself is used to edit a file. The "Compile" button is used to compile it using the java compiler (after you have saved any changes you have made to the file.) The "Run" button is used to cause the Java Virtual Machine to execute a compiled program. Note that the "Run" button will be "grayed out" when it is not applicable - e.g. when there is no source file, the source file has not been compiled, or the source file has been changed since it was last compiled.


Editing A File

You can create a new file by simply typing it in the untitled window that appears when you launch the program (without dragging a file to it). If you wish to create a new, empty window you can simply choose "New" from the File menu.

You can open an existing file to make changes to it by choosing "Open" from the File menu, or by dragging it to the JJEdit icon on the dock.

You can save an edited file by choosing "Save" from the file menu. If the file is new, a save dialog will be displayed to allow you to specify its name and location; otherwise, the previous version will be replaced. Of course, you must save a file before you can compile it. Be sure to save newly created source files on your server volume!

The editor window has a basic set of text-editing tools, such as cut and paste, and a simple search capability (Find). It is possible to use the Preferences dialog (under the JJEdit menu) to change the font and/or size used - but note that the font and size you choose applies to the entire file - it is not possible to have multiple fonts or sizes within a single file. (JJEdit is a simple text editor, not a word processor! But plain text is what the java compiler wants anyway.)

The Preferences dialog also allows you to turn on or turn off three other capabilities: Auto-completion of class names, code coloring, and automatic indentation of code.

If you need to make use of more sophisticated text-processing capabilities, you can make use of another editor such as BBEdit Lite (whose icon also appears on the dock.) It has sophisticated facilities for tabbing, search and replace, and the like. To learn more about its capabilities, see the BBEdit Lite manual. (Note that JJEdit is a java-specific tool, while BBEdit Lite is a general-purpose text editor with no java-specific features.)


Compiling Java source Files

To compile a Java source file, simply click the "Compile" button in its window.

Note that this attempts to compile this file only. However, if your program consists of several source files, this may result in other files being compiled as well. If you compile a file, and it depends upon another file that has not yet been compiled (no .class file exists), then the compiler will compile that file as well. Thus, for example, if class Foo uses class Bar, and class Bar, in turn, uses class Baz, and no file has as yet been compiled, if you click "Compile" in the window for Foo.java, then the other two classes would be compiled as well when compiling class Foo. Thus, in most cases, it suffices to compile the main program and all the other source files will be compiled as well, since the main program presumably depends (directly or indirectly) on all the other classes.

If you change one of the classes, though, you should recompile that class by clicking the Compile button in its window. (If other classes are affected by the change, then you may need to recompile them as well.) Again, the java compiler helps you out a bit here. If you recompile a file that depends on another class, and the source for that class has been modified since it was last compiled, then the other class is compiled as well. However, the compiler only detects this "one deep" - e.g. say you modify Foo and Baz (but not Bar); suppose further that Foo depends only on Bar and Bar depends only on Baz. Then you would have to explicitly recompile Baz as well, because the compiler would not detect the need to do so when recompiling Foo. If there are any syntax errors in your program, one or more error messages will be printed in a window that will appear below the window for your source code. This is illustrated below. The source program is the one that was shown in the previous diagram. You may have noticed that println was misspelled as prinntln, which caused the compiler to report the error shown. ("Cannot resolve symbol" is the compiler's way of saying "I don't know what this word means.")

If no such window appears, then the compilation was successful. (No news is good news :-) (The "Compile" button will be highlighted during compilation, so you can see when the process is done.) Of course, if the compiler reports any errors during compilation, you must correct them and then recompile the program.


Running Compiled Programs

Once you have successfully compiled a java program, you can run it by clicking the "Run" button. (If your program consists of several classes, you must do this in the window for the class containing the main method)

Running a program will create a window that will show console output (output written to System.out) and will also allow console input - i.e. anything you type in the box at the bottom of this window can be read from System.in. If your java program creates a window on the screen, the window will also appear shortly after you issue the command to run the program. The example below shows the execution of the (corrected) program used in the previous illustrations, which simply prints a line of output to System.out.

You can terminate your program in one of three ways:

Having compiled a program successfully, you can run it as many times as you want by clicking the "Run" button each time. You do not need to re-compile the program until you change it.


DON'T FORGET TO LOG OUT BEFORE LEAVING THE LAB!


Last revised: January 17, 2003