Any way to invoke java program directly under CGI?

Discussion in 'Java' started by RobertMaas@YahooGroups.Com, Sep 1, 2003.

  1. Guest

    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.)
     
    , Sep 1, 2003
    #1
    1. Advertising

  2. Chris Uppal Guest

    LDV wrote:

    > > 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?

    >
    > 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
     
    Chris Uppal, Sep 2, 2003
    #2
    1. Advertising

  3. Scott Yanoff Guest

    wrote:
    > 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?


    --
    -Scott
    | http://www.yanoff.org | AOL IM: SAY KJY
     
    Scott Yanoff, Sep 3, 2003
    #3
  4. Guest

    {{Date: Tue, 2 Sep 2003 16:25:34 +0100
    From: "Chris Uppal" <-THIS.org>
    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
     
    , Sep 8, 2003
    #4
  5. Guest

    {{Date: 2 Sep 2003 07:37:55 -0700
    From: (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)?
     
    , Sep 8, 2003
    #5
  6. Guest

    {{Date: Wed, 03 Sep 2003 14:01:44 -0500
    From: Scott Yanoff <>
    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?
     
    , Sep 8, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Malik Asif Joyia
    Replies:
    2
    Views:
    1,117
    =?Utf-8?B?ZGFuaWVs?=
    Aug 12, 2005
  2. news.rcn.com
    Replies:
    1
    Views:
    9,815
    Chris Smith
    Jul 12, 2004
  3. john san
    Replies:
    19
    Views:
    776
    Diez B. Roggisch
    Feb 18, 2005
  4. Shawn
    Replies:
    2
    Views:
    1,572
    Shawn
    Dec 6, 2006
  5. kath
    Replies:
    4
    Views:
    816
    J. Gleixner
    Apr 9, 2007
Loading...

Share This Page