Runtime exec problems with process with multiple args

Discussion in 'Java' started by Bob Connell, Mar 3, 2004.

  1. Bob Connell

    Bob Connell Guest

    I have a C executable program which takes several arguments that I want
    to be able to spawn off using the Java Runtime and Process classes on a
    Windows XP workstation.

    Failed Solution 1:
    ----------------
    I have found that if I start my process as such...

    pe = runt.exec("cmd.exec /c myprocess.exe -arg1 -arg2");

    (where runt is: Runtime runt = Runtime.getRuntime(); )

    .... there are no problems with the execution but I cannot kill my
    process (which may be needed) with the Process destroy() method. It
    seems to kill the cmd.exe but the sub-sub process (e.g., myprocess.exe)
    continues to run.

    Failed Solution 2:
    ------------------

    I have also found that if I run as the following (without the cmd
    command)...

    pe = runt.exec("myprocess.exe -arg1 -arg2");

    My process will hang but I can kill it. I am assuming that it hangs
    because it does recognize the arguments

    Anyone have a solution to this problem or what I should attempt next?

    NOTE: I have tried organizing the command into an array without any
    success.

    thanks!

    Bob
     
    Bob Connell, Mar 3, 2004
    #1
    1. Advertising

  2. "Bob Connell" <> wrote in message news:...
    I have a C executable program which takes several arguments that I want to be able to spawn off using the Java Runtime and Process classes on a Windows XP workstation.
    Failed Solution 1:
    ----------------
    I have found that if I start my process as such...

    pe = runt.exec("cmd.exec /c myprocess.exe -arg1 -arg2");

    (where runt is: Runtime runt = Runtime.getRuntime(); )

    ... there are no problems with the execution but I cannot kill my process (which may be needed) with the Process destroy() method. It seems to kill the cmd.exe but the sub-sub process (e.g., myprocess.exe) continues to run.

    Failed Solution 2:
    ------------------

    I have also found that if I run as the following (without the cmd command)...

    pe = runt.exec("myprocess.exe -arg1 -arg2");

    My process will hang but I can kill it. I am assuming that it hangs because it does recognize the arguments

    Anyone have a solution to this problem or what I should attempt next?

    NOTE: I have tried organizing the command into an array without any success.

    thanks!

    Bob

    Does your executable generate any data on standard output or standard error? If you do not gobble up that output, it may fill the process's buffer and your executable will hang. Also, looking at the output may help you figure out what's actually wrong.

    Cheers,
    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Mar 3, 2004
    #2
    1. Advertising

  3. Bob Connell

    Rajesh Patel Guest

    I have seen programs that expect you to read the
    output of STDERR and STDOUT.

    For example, perl will print X bytes of data to
    it's STDOUT and will halt until somebody reads it.

    The best way to do this is to spawn off a separate thread
    to read the STDERR and STDOUT from your process.

    If this is your problem it explains why CMD is working because
    it's doing that for you.

    Also, I would avoid string version of Runtime.getRuntime().exec like
    the plague. You're not dealing with a command interpreter so it
    will break the following:
    someprog.exe -arg1="c:\Program Files\someArg"

    into

    exe=someprog.exe
    arg1=-arg1="c:\Program
    arg1=Files\someArg"


    not exactly what you would expect.
    The array version does not have this problem.

    Raj

    Bob Connell wrote:

    > I have a C executable program which takes several arguments that I want
    > to be able to spawn off using the Java Runtime and Process classes on a
    > Windows XP workstation.
    >
    > Failed Solution 1:
    > ----------------
    > I have found that if I start my process as such...
    >
    > pe = runt.exec("cmd.exec /c myprocess.exe -arg1 -arg2");
    >
    > (where runt is: Runtime runt = Runtime.getRuntime(); )
    >
    > ... there are no problems with the execution but I cannot kill my
    > process (which may be needed) with the Process destroy() method. It
    > seems to kill the cmd.exe but the sub-sub process (e.g., myprocess.exe)
    > continues to run.
    >
    > Failed Solution 2:
    > ------------------
    >
    > I have also found that if I run as the following (without the cmd
    > command)...
    >
    > pe = runt.exec("myprocess.exe -arg1 -arg2");
    >
    > My process will hang but I can kill it. I am assuming that it hangs
    > because it does recognize the arguments
    >
    > Anyone have a solution to this problem or what I should attempt next?
    >
    > NOTE: I have tried organizing the command into an array without any
    > success.
    >
    > thanks!
    >
    > Bob
    >
     
    Rajesh Patel, Mar 3, 2004
    #3
  4. Bob Connell wrote:

    > I have a C executable program which takes several arguments that I want
    > to be able to spawn off using the Java Runtime and Process classes on a
    > Windows XP workstation.
    >
    > Failed Solution 1:
    > ----------------
    > I have found that if I start my process as such...
    >
    > pe = runt.exec("cmd.exec /c myprocess.exe -arg1 -arg2");
    >
    > (where runt is: Runtime runt = Runtime.getRuntime(); )
    >
    > ... there are no problems with the execution but I cannot kill my
    > process (which may be needed) with the Process destroy() method. It
    > seems to kill the cmd.exe but the sub-sub process (e.g., myprocess.exe)
    > continues to run.
    >
    > Failed Solution 2:
    > ------------------
    >
    > I have also found that if I run as the following (without the cmd
    > command)...
    >
    > pe = runt.exec("myprocess.exe -arg1 -arg2");
    >
    > My process will hang but I can kill it. I am assuming that it hangs
    > because it does recognize the arguments


    It would be wise to investigate the reason for the failure more fully.
    In my opinion it is unlikely that the problem has anything to do with
    the arguments if the specified command would be valid at a shell prompt.

    > Anyone have a solution to this problem or what I should attempt next?


    You should read the Process' output and error streams to look for
    diagnostic messages. Depending on the external program, it may be the
    case that it has run to completion and is a zombie -- to clear it you
    might need to execute the Process' waitFor() method. Do read the API
    docs for the Process class.

    My best guess as to what is actually happening is that the external
    executable is not found in whatever executable path is in effect, which
    may be different from the one set up by a CMD shell.

    > NOTE: I have tried organizing the command into an array without any
    > success.


    All the more reason to think that the problem is not with the arguments.


    John Bollinger
     
    John C. Bollinger, Mar 3, 2004
    #4
  5. Bob Connell

    nos Guest

    "John C. Bollinger" <> wrote in message
    news:c25gq8$d75$...
    > Bob Connell wrote:
    >
    > > I have a C executable program which takes several arguments that I want
    > > to be able to spawn off using the Java Runtime and Process classes on a
    > > Windows XP workstation.
    > >
    > > Failed Solution 1:
    > > ----------------
    > > I have found that if I start my process as such...
    > >
    > > pe = runt.exec("cmd.exec /c myprocess.exe -arg1 -arg2");
    > >
    > > (where runt is: Runtime runt = Runtime.getRuntime(); )
    > >
    > > ... there are no problems with the execution but I cannot kill my
    > > process (which may be needed) with the Process destroy() method. It
    > > seems to kill the cmd.exe but the sub-sub process (e.g., myprocess.exe)
    > > continues to run.
    > >
    > > Failed Solution 2:
    > > ------------------
    > >
    > > I have also found that if I run as the following (without the cmd
    > > command)...
    > >
    > > pe = runt.exec("myprocess.exe -arg1 -arg2");
    > >
    > > My process will hang but I can kill it. I am assuming that it hangs
    > > because it does recognize the arguments


    Aye that's it, you just have to guess and put in some arguments that this
    sneaky bastard program doesn't recognize.
     
    nos, Mar 3, 2004
    #5
    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. Ken Varn
    Replies:
    2
    Views:
    658
    Ken Varn
    Jun 22, 2005
  2. Hal Vaughan
    Replies:
    11
    Views:
    1,195
    Gordon Beaton
    May 22, 2006
  3. Replies:
    3
    Views:
    538
    David Eppstein
    Sep 17, 2003
  4. Pierre Fortin

    args v. *args passed to: os.path.join()

    Pierre Fortin, Sep 18, 2004, in forum: Python
    Replies:
    2
    Views:
    752
    Pierre Fortin
    Sep 18, 2004
  5. Aryeh M. Friedman
    Replies:
    3
    Views:
    261
    FredK
    Jan 11, 2013
Loading...

Share This Page