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
.



Relevant Pages

  • Re: Redirection issue
    ... 1- execute input commands from standard input, ... the phrase "it does not work anymore" carries very little meaning. ... after compilation and execution of the shell with a simple command like ... all the command in the file are executed as well. ...
    (comp.lang.c)
  • Re: Redirection issue
    ... 1- execute input commands from standard input, ... the phrase "it does not work anymore" carries very little meaning. ... after compilation and execution of the shell with a simple command like ... all the command in the file are executed as well. ...
    (comp.lang.c)
  • Re: shell scripting
    ... and am assuming an sh-compatible shell here.... ... command 1 options arguments ... To run the script, do: ... then you need to use the full path to execute it: ...
    (comp.unix.questions)
  • Re: shell scripting
    ... and am assuming an sh-compatible shell here.... ... command 1 options arguments ... To run the script, do: ... then you need to use the full path to execute it: ...
    (comp.unix.shell)
  • Re: Sleep freezing
    ... > word of a command, and each distinct command becomes a sub- ... > When it says that the arguments take the form of a standard shell ... When you execute a command from the shell is it not the ... instead of whatever commands remain in the calling script. ...
    (comp.unix.shell)