Re: Problem with Runtime.exec() and compound commands in Solaris



On Thu, 12 Apr 2007 14:21:29 +0100, ezjlxp <ezjlxp@xxxxxxxxx> wrote:

I'm trying to use Java's Runtime.exec() to call some unix commands in
an uninstaller. For reasons I don't have time to explain, making a
call to a shell script which executes the unix commands isn't an
option.

In the uninstall process i'm trying to execute the follow unix
compound command:

rmdir /directory >> /tmp/uninstall.log 2>&1

if I run this command from a terminal it runs as expected. If the
directory isn't empty it prints a message to the uninstall.log file.
However, when I try to pass this into the exec command it never writes
the error message to the log file when the directory is not empty.

You need a shell to interpret the String that you are trying to execute. The IO redirection is provided by the shell. Instead you need to deal with the IO yourself from Java (via the returned Process object).

Alternatively, you can execute the shell with the -c switch. Something like this:

Runtime.exec("sh -c \"rmdir /directory >> /tmp/uninstall.log 2>&1\"");

Dan.


--
Daniel Dyer
http://www.uncommons.org
.