Hi Derek,

On Tue, 2003-12-09 at 21:01, Derek Brinson wrote:
> Where might I find reference (conceptual) stuff about how to launch a
> JAVA app via CGI (or vice versa)? 

Could you specify what kind of Java application you're talking about?
The Java *applications* (as opposed to servlets, EJBs etc.) I know can
be invoked via the command line.

> Still working it out, but it appears that I may need to get some CGI
> variables into a JAVA App.

If it's a normal application, it should be quite easy - something like
this (roughly sketched and not syntax-checked):

use CGI;

my $query = new CGI;

# this is the actual HTML form - could be out-sourced with 
# HTML::Template or something like that in order not to mix 
# HTML and your code too much
my $HTML_FORM = <<END
<form>
<input type="text" name="java_class_name"/><br/>
<input type="text" name="argument1"/><br/>
<input type="submit" name="btn" value="go"/>
</form>

END

# get the parameters from the CGI query
my $class_name = $query->param('java_class_name');
my $arg = $query->param('argument1');

# start printing the response
print $query->header();
print $query->start_html();

# have we been called with parameters (i.e. did the user press the
button?)
if ($class_Name) {
        # assemble the command line to execute:
        # you should be able to find the java executable in     
        # $JAVA_HOME/bin
        # (this assumes that JAVA_HOME is set, of course, but
        #  normally this should be the case)
        my $command = $ENV{'JAVA_HOME'} . '/bin/java ';
        $command .= $main_class . ' ' . $arg;

        # call java to execute your classes "main" method
        my $result = system($command) / 256;

        # print the result to the browser
        print "invoked the java application.<br/>\n";
        print "the result is " . $result . "<br/>\n";
}
else {
    # no parameters --> display the form
    print $HTML_FORM;
}

print $query->stop_html();

This is more or less it - I'm doing something like this in one of my
scripts, and it's working reasonably well; there are of course some
minor issues you've got to think about, such as:
- do you need libraries to include into your classpath?
(if yes, you can specify them using the -cp argument to java)
- Do you need to get a result from the Java application? 
Does it print stuff into the console? Does it have a return value?
- How long does the application take to execute? In my case, the
application can take more than 1 hour to complete, so I fork and inform
the user per mail about the result.

HTH,

Philipp







-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to