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



Ingo Menger wrote:

On 29 Nov., 13:23, Lionel <lione...@xxxxxxxxx> 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@xxxxxxxxxxxx
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
.



Relevant Pages

  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)
  • Re: passing a string to a dll
    ... Joe, I really appreciate you taking the time to demonstrate this. ... sure how I would implement indexing it for random alphanumeric codes. ... I might handle the array. ... I actually have been wondering if I could use a second string ...
    (microsoft.public.vc.mfc)
  • Re: passing a string to a dll
    ... I might handle the array. ... I actually have been wondering if I could use a second string ... look at insertion cost, organization cost, and search cost. ...
    (microsoft.public.vc.mfc)
  • RE: Structure conversion from C++ to VB-2008?
    ... One of the most important structures is AmiVar structure. ... point number, the array of floating point numbers, a string or IDispatch ... Dim 13012679 as Integer ...
    (microsoft.public.dotnet.languages.vb)
  • Structure conversion from C++ to VB-2008?
    ... One of the most important structures is AmiVar structure. ... point number, the array of floating point numbers, a string or IDispatch ... Dim 13012679 as Integer ...
    (microsoft.public.dotnet.languages.vb)