Execute perl in java

Discussion in 'Java' started by Tim, Dec 13, 2003.

  1. Tim

    Tim Guest

    Hi,
    I need to execute a perl script from within a java program. The
    perl script reads a text file, formats it, then writes it into another
    text file. My code for executing the perl script doesn't work at all:

    try
    {
    Process p = Runtime.getRuntime().exec("C:\\perl\\transReader.pl");
    }
    catch(IOException e)
    {
    System.out.println(e);
    }

    When this is run, no errors are thrown but the text file that is
    supposed to be written to by the perl script remains empty. If anyone
    can help me I'd be extremely grateful.

    Thanks, Hill.
     
    Tim, Dec 13, 2003
    #1
    1. Advertising

  2. Tim:

    [...]

    >When this is run, no errors are thrown but the text file that is
    >supposed to be written to by the perl script remains empty. If anyone
    >can help me I'd be extremely grateful.


    The .pl file is not an executable. Either use "start script.pl" (or
    "cmd /c start script.pl", Google will come up with more suggestions)
    if the file extension .pl is associated with Perl by the operating
    system, or use the perl interpreter as first argument (something like
    "perl script.pl", I don't know the exact syntax for running
    interpreter).

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Dec 13, 2003
    #2
    1. Advertising

  3. Tim

    Tim Guest

    > The .pl file is not an executable. Either use "start script.pl" (or
    > "cmd /c start script.pl", Google will come up with more suggestions)
    > if the file extension .pl is associated with Perl by the operating
    > system, or use the perl interpreter as first argument (something like
    > "perl script.pl", I don't know the exact syntax for running
    > interpreter).


    Thanks for the reply Marco,
    I've tried using the following two lines of code, but I'm still
    having no luck:

    Process p = Runtime.getRuntime().exec("C:\\Perl\\perl
    transReader.pl");

    Process p = Runtime.getRuntime().exec("cmd /c C:\\Perl\\perl
    transReader.pl");

    I really can't see what I'm doing wrong, if anyone knows, or knows of
    a tutorial I could read, please post.
    Thanks...
     
    Tim, Dec 13, 2003
    #3
  4. "Tim" <> wrote in message
    news:...
    > >
    > > The .pl file is not an executable. Either use "start
    > >script.pl" (or "cmd /c start script.pl", Google will come
    > > up with more suggestions) if the file extension .pl is associated
    > > with Perl by the operating system, or use the perl interpreter
    > > as first argument (something like "perl script.pl", I don't know
    > > the exact syntax for running interpreter).

    >
    > Thanks for the reply Marco,
    > I've tried using the following two lines of code, but I'm
    > still having no luck:
    >
    > Process p = Runtime.getRuntime().exec("C:\\Perl\\perl
    > transReader.pl");
    >
    > Process p = Runtime.getRuntime().exec("cmd /c C:\\Perl\\perl
    > transReader.pl");
    >
    > I really can't see what I'm doing wrong, if anyone knows,
    > or knows of a tutorial I could read, please post.
    > Thanks...
    >


    Your launching of the Perl script appears fine. Sometimes a problem that
    might be experienced when using 'exec' is with the process blocking because
    it generates console output that is then not processed [i.e. read by the
    invoking Java program], or, it might be waiting for console input. The way
    to remedy this would be to redirect all console output to NUL:

    String cmdString
    = "cmd.exe /c perl.exe script.pl 2> nul: 1> nul:";

    Process p = Runtime.getRuntime().exec(cmdString);

    and ensure there are no input pending in the script.

    This is not the problem in your program since it is not blocking, but
    quickly returning. Other things to check:

    * Perl executable can be found from Java program launch
    location i.e. it is in the PATH [should be ok since you
    specify the full path]

    * The script can be located. The above code is assuming
    that it is located in the Java program launch location -
    you may wish to ensure that this is so.

    Alternatively, use:

    String cmdString
    = "cmd.exe /c perl.exe -S script.pl 2> nul: 1> nul:";

    Process p = Runtime.getRuntime().exec(cmdString);

    to locate the script in the PATH

    * The final possibility is that the script is broken - it may
    be that it can't locate the files it is supposed to use ?
    Ensure these all use absolute paths, rather than peforming
    any changing of directories

    I hope this helps.

    Anthony Borla
     
    Anthony Borla, Dec 13, 2003
    #4
  5. Also, don't forget to wait for the Process to finish before you try to
    use the results. You do this with Process.waitFor(). Be certain in any
    event to waitFor the process at some point, as otherwise you have a
    resource leak.


    John Bollinger
     
    John C. Bollinger, Dec 15, 2003
    #5
  6. Tim

    kmonsoor

    Joined:
    Feb 10, 2011
    Messages:
    1
    the secret is in another place; not in the command

    i was stuck onto that same problem for more than a week. I even tried the newer method, ProcessBuilder. But alas, all effort went in vain. :(

    Now i discovered that, whatever file the Perl script has created or worked on, has been created in the Java working folder. just refer to that file as './myPerlCreatedFile.ext'

    now :ciao:
     
    kmonsoor, Feb 10, 2011
    #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. lord0
    Replies:
    0
    Views:
    891
    lord0
    Apr 7, 2004
  2. crazykiller

    Execute perl in Java

    crazykiller, Jun 30, 2010, in forum: Java
    Replies:
    0
    Views:
    639
    crazykiller
    Jun 30, 2010
  3. Savas Ates
    Replies:
    1
    Views:
    290
    Patrice
    Aug 17, 2004
  4. Petterson Mikael

    Execute another perl script from my perl script

    Petterson Mikael, Jan 5, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    142
    Paul Lalli
    Jan 5, 2005
  5. Replies:
    20
    Views:
    750
    Gunnar Hjalmarsson
    Jan 18, 2005
Loading...

Share This Page