Runtime exec problems with process with multiple args

B

Bob Connell

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
 
M

Matt Humphrey

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 (e-mail address removed) http://www.iviz.com/
 
R

Rajesh Patel

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
 
J

John C. Bollinger

Bob said:
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
(e-mail address removed)
 
N

nos

Aye that's it, you just have to guess and put in some arguments that this
sneaky bastard program doesn't recognize.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top