Today, I began to wonder if it was possible to use Java in a CGI environment. I then realized, of course you can. Nearly anything is possible via CGI when using a shell script wrapper. I wrote a simple cgi script which executes a java class java-test.cgi:

[noland@a90 cgi-bin]$ cat java-test.cgi
#!/bin/sh
/usr/java/java1.5/bin/java CgiJavaTest

Here is the source of the CgiJavaTest class:

[noland@a90 cgi-bin]$ cat CgiJavaTest.java
public class CgiJavaTest {
        public static void main(String[] args) {
                System.out.println("Content-Type: text/plain; charset=UTF-8\n");
                System.out.println("It worked!");
        }
}

Of course, creating a JVM is pretty expensive….

4 Responses to “Utilizing Java in a CGI environment”

  1. Cattechnologeis Says:

    Utilizing Java in a CGI environment can show wonder.

  2. Roman Says:

    Ain’t tomcat cheaper?

  3. thecubic Says:

    Check out servlets and servlet containers like websphere / apache tomcat / jboss - it’s like this but with big brass ones…

  4. admin Says:

    @Roman

    Much so. You’d have to really want to use some java class here and not be able to use tomcat before you’d do this. I was just showing its possible.

Leave a Reply

If Wordpress eats your comment (shell output, loops, ex..) email the text to me.