Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ProcessBuilder run of a shell wont return failure exit values

807605Sep 11 2007 — edited Sep 26 2007
I am making a call to the shell via ProcessBuilder like this:

ProcessBuilder pb = new ProcessBuilder("ksh","-c","exit","1");
Process proc=pb.start();
int exitVal = proc.waitFor();

The exit value in exitVal is always zero ( 0 ).

But, if I do this:

ProcessBuilder pb = new ProcessBuilder("ksh","-c","false");
Process proc=pb.start();
int exitVal = proc.waitFor();

I appropriately get a one ( 1 ) back from the shell. And if I use "true" instead of "false", I appropriately get a zero ( 0 ).

I am not really doing these calls directly in production code, these calls are from tests while trying to narrow down calls via the shell with "command substitution"; backquote also know as $(<command>).

I am posting these simple tests for ease of discussion.

The shell seems to process the 2nd ProcessBuilder parameter as a command, but any parameters beyond the second are not being passed to the command in the shell, but instead passed to the shell itself.

Has anyone successfully used ProcessBuilder to go into the shell and process a bunch of stuff and get the exit code from that stuff? This is a conversion from Runtime.exec() that currently calls the shell and appropriately passes back whatever occured in the shell.

TIA !
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 10 2007
Added on Sep 11 2007
3 comments
159 views