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

E

ezjlxp

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
 
G

Gordon Beaton

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?

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

--
 
A

Andrew Thompson

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.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top