Working without BlueJ
(command line programming; the main method)

Using BlueJ as our Java development platform has offered us many conveniences. It provides a nice, graphical user interface which integrates the control and editing of the source code files, a compilation process in which errors are highlighted in the original source code, and the ability to create and inspect objects interactively or to execute a program via the debugger. There are many other such IDEs (integrated development environments) which offer similar conveniences.

In actuality, BlueJ is software which is sitting on top of a lower-level Java Software Development Kit (SDK) which is provided by Sun Microsystems.

Our goal at this point is to see how to live without BlueJ and instead to rely directly on the SDK when working with a "command line prompt" in a "terminal" window. There are three distinct issues to address towards this end:

  1. Basics of Linux (patel2's operating system), and the command line prompt.
  2. Basics of the Java SDK
  3. Significance of the main method


Basics of Linux

On patel2, you can open up a terminal window by clicking on an icon of an old-fashioned computer monitor at the bottom of the screen (it is the fourth icon from the left). Alternatively, you can go to the start menu, the System submenu, and select Konsole (Terminal Program). At this point, a new window should appear allowing you to type in commands at what is known as the "command line prompt" or sometimes known as the "shell prompt."

Bare Bones Linux commands:

In addition, we need an editor to allow us to create or modify text files to be used as Java source code or as program input. There are many editors available on our system, some of which can be used within the terminal screen (e.g., emacs, nano, vi). For sanity's sake, I would suggest that we continue to use a graphical editor for convenience. Several selections are available from the "Editors" submenu of the "Start" menu at bottom-left.

Basics of Java SDK


Significance of the main method

BlueJ offered us the convenience of a point-and-click way to begin the execution of any class method. When working at the command-line, we do not have this flexibility. In particular, the only method we can execute directly is a class method with the following precise declaration:


public static void main(String[] args) {

}
This is the so called main method for a class. Notice that the keyword static is used to designate this as a class method; this allows it to be executed even in advance of the existence of any objects from the class. The method does not have any return value. You may also note that the method accepts a single parameter, which is an array of Strings. These strings are commonly called the "arguments" or "command line arguments."

For example, let's assume that we have a class named Example with such a main method. Then we could execute that main method by typing the following at the command line prompt:

   java Example
In this case (the default), the array args is actually an array of length zero. However, the user can also specify any number of additional arguments on the command line, with whitespace used to separate such arguments. Therefore, if the user types the following:
   java Example bob 5 how_are_you
The array args will be initializes with length 3, and with the three strings {"bob", "5", "how_are_you"}.

The purpose of allowing command line arguments is for convenience in specifying some controlling parameters for programs. Of course programs can always be written so as not to rely on command line arguments, but instead to prompt the user to enter information as the program is running.


Michael Goldwasser
Last modified: Friday, 16 April 2004