Java CGI HOWTO: Executing a Java CGI Program

Next Previous Contents

## 4. Executing a Java CGI Program

## 4.1 Obstacles to Running Java Programs Under the CGI Model

There are two main problems in running a Java program from a web server:

### You can't run Java programs like ordinary executables.

You need to run the Java run-time interpreter and provide the initial class (program to run) on the command-line. With an HTML form, there is no provision for sending a command-line to the web server.

### Java does not have general access to the environment.

Every environment variable that will be needed by the Java program must be explicitly passed in. There is no method similar to the C `getenv()` function.

## 4.2 Overcoming Problems in Running Java CGI Programs

To deal with these obstacles, I wrote a shell CGI program that provides the information needed by the Java interpreter.

### The java.cgi script.

This shell script manages the interaction between the HTTP daemon and the Java CGI program that you wish to use. It extracts the name of the program that you want to run from the server-provided data. It collects all of the environment data into a temporary file. Then, it runs the Java run-time interpreter with the name of the file of environment information and the program name added to the command-line.

The `java.cgi` script was configured and installed in Decide On Your Local Path Policies.

### Invoking java.cgi from an HTML form.

My forms that use Java CGI programs specify a form action as follows:

    <form action="/cgi-bin/java.cgi/CGI_Test" method="POST">

Where `/cgi-bin/` is your local CGI binary directory, `java.cgi` is the Java front-end that allows us to run Java programs over the web and `CGI_Test` is an example of the name of the Java program to run.

Next Previous Contents