rohitb has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am calling a Java class file through my script. I using syntax .... system("java JavaClassFile"); This syntax is working, if I am executing this script on command line. BUT, if I run it through browser(as CGI script), it doesn't work. Any suggestion, how to run it through browser? Thanks in advance. -Rohit

Replies are listed 'Best First'.
Re: Calling Java through CGI Script
by tirwhan (Abbot) on Nov 11, 2005 at 11:33 UTC

    Take a look at your web server's log, there's likely to be a helpful error message in there somewhere. As a first shot, try using an absolute path in your system() call, e.g.


    (adjust path as appropriate). Note that there's probably a better way to reach your goal unless this is just a quick'n dirty solution you're developing (for example Inline::Java allows you to put Java code directly into your perl script).

    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: Calling Java through CGI Script
by davorg (Chancellor) on Nov 11, 2005 at 11:34 UTC

    Please give a bit more detail than simply "it doesn't work". What unexpected behaviour are you seeing? What behaviour did you expect to see? Are you getting any error messages? Is anything written to the web server error log?

    It's probably a difference between a process running under your username and the web server process that runs under a different username - that almost certainly has different permissions on files and a whole different environment set up. The most obvious suspects I can see are that the web server user doesn't have the "java" command in it's path or that the JavaClassFile isn't in the current directory.

    But without a more detailed description of your problem, it's impossible to do more than guess.


    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Calling Java through CGI Script
by rcseege (Pilgrim) on Nov 11, 2005 at 11:48 UTC

    Just a stab in the dark, but I would guess that this is a CLASSPATH issue. You can pass the classpath via the command line or set the environment variable. The following syntax is used to set the CLASSPATH via the command line:

    java -cp dir/containing/package package.ClassFile java -classpath dir/containing/package package.ClassFile
Re: Calling Java through CGI Script
by tbone1 (Monsignor) on Nov 11, 2005 at 14:49 UTC
    If you are on a Unix-based system, check your CLASSPATH environment variable within the CGI script. Just the other day a coworker couldn't understand a problem because they could run a script from the command line but it crashed on our production server; the reason was that he had set his own private environment variable but the production environment didn't have it set. And boy, was that ever a pain to track down!

    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

Re: Calling Java through CGI Script
by cbrandtbuffalo (Deacon) on Nov 11, 2005 at 17:12 UTC
    If you think you might need to do this often, you should check out Inline::Java. You can find some other info about that module on here and here.
Re: Calling Java through CGI Script
by blazar (Canon) on Nov 11, 2005 at 15:01 UTC
    I think that you have to put relevant code in the generated HTML to instruct the browser to download the class and (have the local Java runtime engine) execute it.