Re: Executing command with Runtime.getRuntime.exec() fails



Gordon Beaton wrote:
On Thu, 29 Nov 2007 21:56:14 +1000, Lionel wrote:
If I change mysqlServiceCommand to
"cmd /c \"C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqld-nt.exe
--install MySQL1";

It works as expected.

Does anyone have any idea what's going on?

The string you pass to exec(String) gets tokenized before it's passed
to exec(String[]). The tokenizer breaks your command into whitespace
delimited tokens without regard to escaping, quoting or other special
characters.

In your particular example, you pass your arguments to a command shell
which does further processing to the command line (and probably
respects escapes, quotes etc) before the command itself gets executed
by the shell you specified.

Exactly how the two interact is not always clear, as you've
discovered. Try running a program that simply prints its arguments to
get a better understanding of the situation. Try it both with and
without "cmd /c".

If you want a greater degree of control over this, you can avoid the
initial tokenization done by exec(String) by instead calling
exec(String[]) directly. Break the command into tokens yourself
(exactly one per array element) without using extra quotation marks.


More detailed response of what Patrick also said, so I'll answer both here.

I did in fact try using exec(String[]) but perhaps I tokenized it incorrectly. This is what I tried:

String mysqlServiceCommand[] = {"cmd", "/c",
"C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqld-nt.exe\"",
"--install", "MySQL1",
"--defaults-file=\"C:\\Program Files\\MySQL\\MySQL Server " + "5.0\\my-large.ini\""};

This give the same error as before!

Do I need to tokenise at spaces?

thanks

Lionel.
.



Relevant Pages

  • Re: Executing command with Runtime.getRuntime.exec() fails
    ... The tokenizer breaks your command into whitespace ... delimited tokens without regard to escaping, ... you pass your arguments to a command shell ...
    (comp.lang.java.programmer)
  • Re: How to find total MB of a directory plus all subdirectories
    ... On Sunday 11 March 2007 22:06, Les wrote: ... The reason a command won't parse correctly with a space in the file ... name is due to the tokenizer used by the shell. ... I posted also to the KDE list to see if Konqueror's file manager would show ...
    (Fedora)
  • Re: String array not recognized
    ... problem was that the tokenizer wasn't working but I get the same error. ... and develop some Java programs meant to be run from the command ... Command line programs ... Andrew T. ...
    (comp.lang.java.programmer)