Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

6 views
Skip to first unread message

ezjlxp

unread,
Apr 12, 2007, 9:21:29 AM4/12/07
to
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
run into an error, will not print the error message to the
uninstall.log file.

Any help would be greatly appreciated. Thank you!

-Jared

Daniel Dyer

unread,
Apr 12, 2007, 9:46:49 AM4/12/07
to
On Thu, 12 Apr 2007 14:21:29 +0100, ezjlxp <ezj...@gmail.com> 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

Andrew Thompson

unread,
Apr 12, 2007, 10:17:11 AM4/12/07
to
On Apr 12, 11:15 pm, "ezjlxp" <ezj...@gmail.com> wrote:
..
> 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.

ezjlxp

unread,
Apr 12, 2007, 10:23:25 AM4/12/07
to

Thanks Dan! I'll give that a try.

Eric Sosman

unread,
Apr 12, 2007, 10:47:49 AM4/12/07
to
ezjlxp wrote On 04/12/07 09:21,:

> 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.
> [...]

Multiple choice: When you run the command from a
terminal, what agency arranges for the I/O redirection?

a) The rmdir program executable
b) The Unix kernel
c) The National Security Agency
d) Windows Media Player
e) The shell

True or false: The rmdir program automatically runs
a shell.

Essay question: Describe how to start a shell and
instruct it to execute one command, with all the I/O
redirection, filename globbing, and other goodies one
usually expects from a shell.

> Any help would be greatly appreciated. Thank you!

You're welcome. I hope this helps.

--
Eric....@sun.com

0 new messages