Any way to invoke java program directly under CGI?

R

RobertMaas

Let's say I have written a "Hello World" java program: h.java
and I've compiled it to: h.class
It emits a HTTP header line, a blank line, then the "Hello World!" line.
Is there any way to directly run that as a CGI script, or do I have to
use a shell script that calls java indirectly?

For example, in PERL, I can make a CGI PERL script directly, like this:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, world!\n";
(That's what I have in http://www.rawbw.com/~rem/cgi-bin/h-perl.cgi now.)

and likewise I can make direct scripts in sh:
#! /bin/sh
echo 'Content-type: text/html'
echo
echo 'Hello World!'
(That's what I have in http://www.rawbw.com/~rem/cgi-bin/h-sh.cgi now.)

But in CMUCL, I have to
make a sh script that indirectly invokes CMUCL, like this:
#! /bin/sh
/usr/local/bin/lisp -eval '(progn (format t "Content-type: text/html~%~%Hello World!~%") (quit))'
(That's what I have in http://www.rawbw.com/~rem/cgi-bin/h-cmucl.cgi now.)

So if my Hello World program is written in java, can I do it directly
like this:
#! /usr/local/jdk1.2.2/bin/java
something... h.class ...something

or do I have to do it indirectly via sh script like this:
#! /bin/sh
/usr/local/jdk1.2.2/bin/java h
(That's what I have in http://www.rawbw.com/~rem/cgi-bin/h-java.cgi now.)
 
C

Chris Uppal

LDV said:
To run your Java CGI directly, you have to use a binary
wrapper/launcher (painfully slow) or compile it to a native binary
using GCJ (available now) or Excelsior JET (Linux version is coming
soon):

If the CGI program does a small enough amount of work for the JVM startup time
to be significant, then it'd be worthwhile trying to run it in pure interpreted
mode (if your particular JVM supports it) or perhaps even use a light-weight
JVM.

Yet another approach would be to check out Fast-CGI (http://www.fastcgi.com/).

-- chris
 
S

Scott Yanoff

Let's say I have written a "Hello World" java program: h.java
and I've compiled it to: h.class
It emits a HTTP header line, a blank line, then the "Hello World!" line.
Is there any way to directly run that as a CGI script, or do I have to
use a shell script that calls java indirectly?

It works for /bin/sh and Perl because those interpret ASCII text whereas
Java wants a class file with bytecode in it. You're stuck calling java
via a shell script. Of course, the output will not go to the web
browser unless you output the proper content type, too. Is there any
reason why you can't write this as a servlet and use a servlet runner or
application server to execute it?
 
R

RobertMaas

{{Date: Tue, 2 Sep 2003 16:25:34 +0100
From: "Chris Uppal" <[email protected]>
If the CGI program does a small enough amount of work for the JVM
startup time to be significant, then it'd be worthwhile trying to run
it in pure interpreted mode (if your particular JVM supports it) or
perhaps even use a light-weight JVM.}}

java -version
java version "1.2.2"
Classic VM (build jdk1.2.2-FreeBSD:root:2000/11/25-02:08, green threads, nojit)

So that version doesn't have the pure interpreted mode, right?

{{Yet another approach would be to check out Fast-CGI
(http://www.fastcgi.com/).}}

That mentions mod_fastcgi, so how do I find out whether my ISP has it
installed/available or not? whereis doesn't even show mod_macro which
it definitely does seem to have:
Server: Apache/1.3.26 (Unix) mod_macro/1.1.1
 
R

RobertMaas

{{Date: 2 Sep 2003 07:37:55 -0700
From: (e-mail address removed) (LDV)
To run your Java CGI directly, you have to use a binary
wrapper/launcher (painfully slow)}}

That sounds worse than just indirecting via a shell script.

{{or compile it to a native binary using GCJ (available now) or
Excelsior JET (Linux version is coming soon)}}

Do you happen to know how to determine whether either is already
installed/available on my ISP (FreeBSD)?
 
R

RobertMaas

{{Date: Wed, 03 Sep 2003 14:01:44 -0500
From: Scott Yanoff <[email protected]>
It works for /bin/sh and Perl because those interpret ASCII text
whereas Java wants a class file with bytecode in it.}}

Too bad Java doesn't accept an ASCII text script that simply tells the
name of the class file to use. I was hoping for some trick like that.

{{You're stuck calling java via a shell script.}}

Yeah, that's what I did prior to asking if I can do it more directly:

1 -rw------- 1 rem user 219 Sep 1 03:44 h.java
class h
{
public static void main(String args[])
{
System.out.println("Content-type: text/html");
System.out.println("");
System.out.println("Hello World!");
}
}

1 -rwxr--r-- 1 rem user 42 Sep 1 03:53 h-java.cgi*
#! /bin/sh
/usr/local/jdk1.2.2/bin/java h

http://www.rawbw.com/~rem/cgi-bin/h-java.cgi

{{Of course, the output will not go to the web browser unless you
output the proper content type, too.}}

Yeah, I already did that, see definition of main in h.java above.

{{Is there any reason why you can't write this as a servlet and use a
servlet runner or application server to execute it?}}

Yes: I don't know whether my ISP supports java servlets, nor how to
find out, and if supported then how to set up the hello program that
way. Do you happen to know either?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top