Calling external programs from within a Tomcat application

Discussion in 'Java' started by pwaring@gmail.com, Jul 10, 2008.

  1. Guest

    I've got a fairly complex existing Tomcat application (which is
    packaged and built as a .war file) which I'm trying to edit so that it
    calls a Perl script part way through the processing, which will
    generate an XML file used later on. I think I've worked out how to
    call external scripts from within Java, but at the moment when I try
    and access the application via Tomcat the application either hangs or
    bails out (I don't know which, as the log files unhelpfully don't give
    any error messages).

    The code which is causing the problem looks like this:

    System.err.println("Calling runtime...");
    Runtime runtime = Runtime.getRuntime();
    System.err.println("Executing process...");
    Process process = runtime.exec("/path/to/ysearch.pl 'News' '\"search
    query\"' 'file");
    System.err.println("Waiting for process...");
    int exitVal = process.waitFor();
    System.out.println("Exited with error code: " + exitVal);

    The code gets as far as "Executing process..", beyond that there is
    nothing in the log file so I presume the runtime.exec() call is where
    the problem is. I'm not interested in reading the output from the
    script (there shouldn't be any), so that's not an issue, and the
    permissions on it allow anyone to read or execute ysearch.pl so I
    don't think there's a problem in that area.

    Does anyone have any suggestions which I could try to get this to
    work? I've only been using Tomcat for a week (I'm picking up on
    someone else's code) so I might have made a beginner's mistake. I'm
    using Tomcat 5 on Fedora Core 7, and Java 1.5.0_01 (I can't easily
    change any of those).
     
    , Jul 10, 2008
    #1
    1. Advertising

  2. shakah Guest

    On Jul 10, 5:03 am, "" <> wrote:
    > I've got a fairly complex existing Tomcat application (which is
    > packaged and built as a .war file) which I'm trying to edit so that it
    > calls a Perl script part way through the processing, which will
    > generate an XML file used later on. I think I've worked out how to
    > call external scripts from within Java, but at the moment when I try
    > and access the application via Tomcat the application either hangs or
    > bails out (I don't know which, as the log files unhelpfully don't give
    > any error messages).
    >
    > The code which is causing the problem looks like this:
    >
    > System.err.println("Calling runtime...");
    > Runtime runtime = Runtime.getRuntime();
    > System.err.println("Executing process...");
    > Process process = runtime.exec("/path/to/ysearch.pl 'News' '\"search
    > query\"' 'file");
    > System.err.println("Waiting for process...");
    > int exitVal = process.waitFor();
    > System.out.println("Exited with error code: " + exitVal);
    >

    [...rest of post snipped...]

    I usually the String-array form of Runtime.exec() to avoid shell
    quoting problems (though that doesn't appear to be your problem here).
    Beyond that, try running the command from a shell prompt while logged
    in as the user Tomcat runs under, maybe you'll find you have a
    LD_LIBRARY_PATH problem or something. FWIW, the code which uses the
    String-array form of Runtime.exec() ends up something like:

    String [] asCmd = new String[4] ;
    asCmd[0] = "/path/to/ysearch.pl" ;
    asCmd[1] = "News" ;
    asCmd[2] = "search query" ;
    asCmd[3] = "filename" ;

    java.lang.Process p = java.lang.Runtime.getRuntime().exec(asCmd) ;
    p.waitFor() ;

    java.io.InputStream is = null ;
    try {
    is = p.getInputStream() ;
    java.io.BufferedInputStream bis = new
    java.io.BufferedInputStream(is) ;

    byte [] ab = new byte[64000] ;
    int nTotalBytesRead = 0 ;
    while(nTotalBytesRead < 64000) {
    int nBytesRead = bis.read(ab, nTotalBytesRead, 64000 -
    nTotalBytesRead) ;
    if(nBytesRead < 0) {
    break ;
    }
    else {
    nTotalBytesRead += nBytesRead ;
    }
    }

    return new String(ab, 0, nTotalBytesRead) ;
    }
    finally {
    if(null != is) {
    try {
    is.close() ;
    }
    catch(Exception x) {
    }
    is = null ;
    }
    }
     
    shakah, Jul 10, 2008
    #2
    1. Advertising

  3. Guest

    On Jul 10, 1:37 pm, shakah <> wrote:
    > I usually the String-array form of Runtime.exec() to avoid shell
    > quoting problems (though that doesn't appear to be your problem here).


    I didn't think that would be the problem either, but after using the
    method you suggested it worked perfectly (though I had to change some
    permissions beforehand) - thanks!

    Paul
     
    , Jul 14, 2008
    #3
  4. tmpuser Guest

    On Jul 14, 12:55 pm, "" <> wrote:
    > On Jul 10, 1:37 pm, shakah <> wrote:
    >
    > > I usually the String-array form of Runtime.exec() to avoid shell
    > > quoting problems (though that doesn't appear to be your problem here).

    >
    > I didn't think that would be the problem either, but after using the
    > method you suggested it worked perfectly (though I had to change some
    > permissions beforehand) - thanks!
    >
    > Paul


    I'm having a similar problem. Can you please tell me what permissions
    did you set up?
    thanks
     
    tmpuser, Jul 25, 2008
    #4
  5. Guest

    On Jul 25, 12:49 pm, tmpuser <> wrote:
    > I'm having a similar problem. Can you please tell me what permissions
    > did you set up?


    I just created a directory called /tomcat/ with the following
    permissions:

    drwxr-xr-x 2 tomcat tomcat 12288 2008-07-28 09:49 tomcat

    Ugly hack I know, but I just needed a quick solution that worked.

    Paul
     
    , Jul 28, 2008
    #5
  6. Guest

    Have you considered running a modified version of the Perl script as a
    CGI?

    My recollection is that Tomcat CGI is turned off by default - but it
    can be configured to be turned on.
     
    , Jul 28, 2008
    #6
  7. Lew Guest

    wrote:
    > No, because I don't know how to do that, either from the Perl side (I
    > only use Perl scripts on the command line) or via Tomcat (which I'm
    > only using for this because of someone else's code - I would never
    > touch it out of choice).


    Project is a proven sling and generally unsuitable to harass as bushes go. I
    wonder what your lighter with it is.

    --
    Lew


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "Degrees do not matter...one does not bargain about inches of evil."

    --- Ayn Rand, Atlas Shrugged
     
    Lew, Jul 29, 2008
    #7
  8. Guest

    On Jul 28, 8:51 pm, ""
    <> wrote:
    > Have you considered running a modified version of the Perl script as a
    > CGI?
    >
    > My recollection is that Tomcat CGI is turned off by default - but it
    > can be configured to be turned on.


    No, because I don't know how to do that, either from the Perl side (I
    only use Perl scripts on the command line) or via Tomcat (which I'm
    only using for this because of someone else's code - I would never
    touch it out of choice).

    Paul
     
    , Jul 29, 2008
    #8
    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. Bob=Moore
    Replies:
    10
    Views:
    2,820
  2. Juggler

    calling external program from within C++

    Juggler, Feb 24, 2005, in forum: C Programming
    Replies:
    5
    Views:
    608
    Nils Weller
    Feb 24, 2005
  3. lone_eagle
    Replies:
    3
    Views:
    670
    psykeedelik
    May 26, 2009
  4. Philippe C. Martin
    Replies:
    5
    Views:
    184
    kaeli
    Apr 18, 2005
  5. Mellow Crow
    Replies:
    6
    Views:
    442
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page