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.
I read that if there is a space in an argument of a command that you
need to put the command into an String array instead of a string.
Therefore I tried the following with no luck:
Process p = new Process();
...
String[] cmd = new String[]{"rmdir", "/directory", ">>", "/tmp/
uninstall.log", "2>&1";
p = Runtime.getRuntime().exec(cmd).waitFor();
is that the correct way to split up the command to put it into the
String array?
I have a hand full of other commands I am calling in the same way.
All of them execute the Unix command as expected but if any of them
fun into an error, will not print the error message to the
uninstall.log file.
Any help would be greatly appreciated. Thank you!
-Jared
Gordon Beaton - 12 Apr 2007 14:38 GMT
> Process p = new Process();
> ...
[quoted text clipped - 4 lines]
> is that the correct way to split up the command to put it into the
> String array?
No.
First, redirection is a shell feature. Runtime.exec() doesn't run your
command in a shell, so you need to include one as part of the command.
Also, the shell expects the entire command to be a single argument (a
single element in the command array).
Do it like this:
String[] cmd = {
"/bin/sh",
"-c",
"rmdir /directory >> /tmp/uninstall.log 2>&1"
};
Don't forget to close the 3 streams associated with the resulting
Process object (whether you're interested in them or not).
/gordon
--
Andrew Thompson - 12 Apr 2007 15:17 GMT
..
> For reasons I don't have time to explain, ..
And yet, strangely, you 'have the time' to
multi-post this message.
Perhaps you should consider getting back to
us when you..
a) can *find* the time.
b) develop some basic *manners*.
(X-post to c.l.j.p./h., w/ f-u to c.l.j.p. only)
Andrew T.