Re: Executing command with Runtime.getRuntime.exec() fails
Ingo Menger wrote:
On 29 Nov., 13:23, Lionel <lione...@gmail.com> wrote:
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\""};
What is the "cmd /c" for, except to make the program less portable?
I think one can trust that Runtime.exec will choose the right command
interpreter.
No, it definitely will not. exec() will run exactly what you tell it to run. If
you don't explicitly include cmd.exe then it will not run cmd.exe. It will
execute the first element from the array if you invoke exec(String[]), or the
first whitespace-delimited sub-string if you invoke exec(String).
Actually, this might be the whole problem, since Runtime.exec will
pass your command string to the system default command interpreter
No it won't.
(which does tokenization). So it ends up like
cmd /c "cmd /c \"your program\""
when you want
cmd /c "your program" (on Windows)
and
sh -c "your prog" (under Linux)
Neither cmd.exe [on Windows] or sh [on Linux] will be exec'd unless you tell
exec() explicitly to do so by including it as the first argument in the string
(or string array) passed to exec.
This give the same error as before!
Do I need to tokenise at spaces?
The problem is that an array of strings is getting passed to cmd.exc. The first
argument is the executable you really want to run, and cmd.exe will attempt to
run it. All the other arguments are passed to cmd.exe, not to mysqld-nt.exe.
You can pass a single string to cmd.exe and let it work out how to execute the
string, but you only need to do this if cmd.exe is actually required. If
cmd.exe isn't needed then remove it from the list and invoke mysqld-nt.exe
directly by making it the first element of the array, with the arguments being
passed one per element in the array.
The argument to exec should be either:
String[] { "cmd.exe", "/c", "executable arg1 arg2 ..."}
or
String[] { "executable", "arg1", "arg2" ... }
--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555