In many ways the command-line approach is more similar to human-to-human interaction than a graphical user interface. We communicate with each other by assembling words into questions and commands - not by drawing pictures with a limited set of choices that can be selected. Granted it is much easier to learn a few simple interactions with a graphical user-interface - however they lack the power that can be achieved with a combination of word symbols. Anyone who has ever watched a skilled unix user can attest to this.

The cline package provides a quick way to write programs that provide flexible interaction with the user. Writing new commands is a simple process of extending a base class and defining a method that will be executed when the name of the command is entered at the prompt. For example, here is the source code for a simple command that asks the user a question:

import edu.uw.cline.BaseCommand;

 * A simple demo command to ask the user a question and print out
 * their response.
public class QuestionCommand extends BaseCommand {

  QuestionCommand() {
    super("question", 1, "questionString" +
          "\n- simple command to ask the user a question" +
          "\n  For example:" +
          "\n  question \"How are you today?\"");

  public void execute(String args[]) {
    String answer = getPrompt().askQuestion(args[0]);
    System.out.println("Your answer was: " + answer);

The BaseCommand constructor requires a name for the command, an integer specifying how many arguments to expect, and a string that will be used for the built-in help system. For example, the user can find out information about the above command as follows:

prompt> help question
(QuestionCommand) usage:
question questionString
- simple command to ask the user a question
  For example:
  question "How are you today?"

As the help command describes, activating the QuestionCommand is as simple as:

prompt> question "What, is your favorite color?"
What, is your favorite color?: blue
Your answer was: blue

A console-based (eg. for use in an xterm or DOS-prompt) and an AWT-based version are provided in the cline package.

ShellPrompt (for use with a DOS-prompt or xterm)

The console version of the command-line prompt uses the default System.out, System.err and streams to interact with the user. Here is the source code for a simple program that creates a ShellPrompt, registers the silly QuestionCommand described above, and then starts the prompt in interactive mode:

TextAreaPrompt (for use with Java's AWT, including Applets)

The TextAreaPrompt provides a means of creating a command prompt within a graphical user interface. This is useful for a number of reasons:

The design goal for the TextAreaPrompt was to create an input and output stream that work within a java.awt.TextArea. This enables the standard, System.out and System.err streams to be redirected, and the same code used for the ShellPrompt can be recycled. Another advantage is that we can use standard calls such as System.out.println() to display messages to the user - we don't require special calls to an AWT component that displays text output.

Here is the source code for a simple AWT-based application that uses the TextAreaPrompt:

applet difficulties

Using the TextAreaPrompt within an Applet poses some security challenges as it requires that standard IO be redirected. The TextAreaPrompt can be used with the Java 1.3 browser plug-in by using policytool (included with the plug-in) to enable the following permission:

permission java.lang.RuntimePermission "setIO"

Alternatively, you can manually add the following lines to a file called ".java.policy" (by default in c:\windows on a Windows machine, or in your $HOME directory for a unix box):

grant {
  permission java.lang.RuntimePermission "setIO";

If you are have the Java 1.3 plug-in (or are willing to download it), set the IO permission and then here is the applet. Note - if you do not have the Java 1.3 plug-in and would like to download it, clicking on the applet link should automatically start the process. A fancier applet that also makes use of the TextAreaPrompt can be found on the symblnet page.

I am still playing with Internet Explorer and Netscape to see if it is possible to use the TextAreaPrompt with the default Java VM.


The cline package can be downloaded as a jar file containing only the class files (cline.jar), or as a jar file containing the documented Java source (cline_src.jar).




Javadocs for the cline package

Recently I came across a related article on the web: Reinventing the Art of Creating Command-line Java Apps

home | projects