Problems with Runtime.exec()

Discussion in 'Java' started by Manish Hatwalne, Jul 21, 2004.

  1. I have a command line client which takes and input file and produces some
    output file, along with few other arguments. I am trying to run this program
    from my Java code. This is what I have done -

    String args[] = {clientPath, "-l", "-a", clientIPAddress, "-u", userName,
    "-p", userPass, "<", inputFile, ">", outputFile};
    Process p = Runtime.getRuntime().exec(args);
    p.waitFor();

    But, it doesn't work (as in the output file is not created). However, if I
    create a batch file with this command line and execute it from the Java
    code, it works fine. What could be the problem???? I have verified that the
    command is the batch file and in Java code is exactly same.

    Where should I look now???

    TIA,
    - Manish
     
    Manish Hatwalne, Jul 21, 2004
    #1
    1. Advertising

  2. Hi Manish!

    Manish Hatwalne wrote:

    >I have a command line client which takes and input file and produces some
    >output file, along with few other arguments. I am trying to run this program
    >from my Java code. This is what I have done -
    >
    >String args[] = {clientPath, "-l", "-a", clientIPAddress, "-u", userName,
    >"-p", userPass, "<", inputFile, ">", outputFile};
    >Process p = Runtime.getRuntime().exec(args);
    >p.waitFor();
    >

    This does not work because the arguments concerning in/out-redirection
    ("<", inputFile, ">", outputFile) also get passed to your executable
    (clientPath). Executables are not capable of interpreting these
    arguments. This is the job of a command-shell ("CMD.EXE" or "/bin/sh",
    depending on OS).

    >But, it doesn't work (as in the output file is not created). However, if I
    >create a batch file with this command line and execute it from the Java
    >code, it works fine. What could be the problem???? I have verified that the
    >command is the batch file and in Java code is exactly same.
    >
    >Where should I look now???
    >
    >TIA,
    >- Manish
    >
    >

    The code below should work. (Note that, now you need spaces for
    separating the command-string):

    // On Windows:
    String args[] = { "cmd", "/c", clientPath + " -l -a " + clientIPAddress
    + " -u " + userName +
    " -p " + userPass + " < " inputFile + " > " + outputFile };
    Process p = Runtime.getRuntime().exec(args);
    .....

    // On Linux/Unix:
    String args[] = { "sh", "-c", clientPath + " -l -a " + clientIPAddress +
    " -u " + userName +
    " -p " + userPass + " < " inputFile + " > " + outputFile };
    Process p = Runtime.getRuntime().exec(args);
    ......

    --
    Thomas<dot>Fritsch<squiggle>ops<dot>de
     
    Thomas Fritsch, Jul 21, 2004
    #2
    1. Advertising

  3. Thanks!!! :) :)
    You were right!! I should have thought of that. I didn't need the spaces
    that you mentioned though!

    rgds,
    - Manish

    "Thomas Fritsch" <> wrote in message
    news:...
    > Hi Manish!
    >
    > Manish Hatwalne wrote:
    >
    > >I have a command line client which takes and input file and produces some
    > >output file, along with few other arguments. I am trying to run this

    program
    > >from my Java code. This is what I have done -
    > >
    > >String args[] = {clientPath, "-l", "-a", clientIPAddress, "-u", userName,
    > >"-p", userPass, "<", inputFile, ">", outputFile};
    > >Process p = Runtime.getRuntime().exec(args);
    > >p.waitFor();
    > >

    > This does not work because the arguments concerning in/out-redirection
    > ("<", inputFile, ">", outputFile) also get passed to your executable
    > (clientPath). Executables are not capable of interpreting these
    > arguments. This is the job of a command-shell ("CMD.EXE" or "/bin/sh",
    > depending on OS).
    >
    > >But, it doesn't work (as in the output file is not created). However, if

    I
    > >create a batch file with this command line and execute it from the Java
    > >code, it works fine. What could be the problem???? I have verified that

    the
    > >command is the batch file and in Java code is exactly same.
    > >
    > >Where should I look now???
    > >
    > >TIA,
    > >- Manish
    > >
    > >

    > The code below should work. (Note that, now you need spaces for
    > separating the command-string):
    >
    > // On Windows:
    > String args[] = { "cmd", "/c", clientPath + " -l -a " + clientIPAddress
    > + " -u " + userName +
    > " -p " + userPass + " < " inputFile + " > " + outputFile };
    > Process p = Runtime.getRuntime().exec(args);
    > ....
    >
    > // On Linux/Unix:
    > String args[] = { "sh", "-c", clientPath + " -l -a " + clientIPAddress +
    > " -u " + userName +
    > " -p " + userPass + " < " inputFile + " > " + outputFile };
    > Process p = Runtime.getRuntime().exec(args);
    > .....
    >
    > --
    > Thomas<dot>Fritsch<squiggle>ops<dot>de
    >
     
    Manish Hatwalne, Jul 22, 2004
    #3
    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 Connell
    Replies:
    4
    Views:
    1,342
  2. Hal Vaughan
    Replies:
    11
    Views:
    1,130
    Gordon Beaton
    May 22, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,095
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    470
    Duncan Booth
    Jul 22, 2004
  5. Memo

    runtime.exec problems

    Memo, Jun 25, 2011, in forum: Java
    Replies:
    2
    Views:
    426
    Roedy Green
    Jun 25, 2011
Loading...

Share This Page