Re: Executing command with Runtime.getRuntime.exec() fails
- From: Nigel Wade <nmw@xxxxxxxxxxxx>
- Date: Fri, 30 Nov 2007 14:16:08 +0000
Ingo Menger wrote:
On 29 Nov., 18:05, Nigel Wade <n...@xxxxxxxxxxxx> wrote:Runtime.exec("ls")
Ingo Menger wrote:
On 29 Nov., 16:30, Nigel Wade <n...@xxxxxxxxxxxx> wrote:
If
you don't explicitly include cmd.exe then it will not run cmd.exe.
Too bad for the "run anywhere" approach. Especially when directory
names contain spaces.
Runtime.exec() is non-portable.
That's exactly my point.
Even if exec() had a notion of "intelligence" and did run cmd
on Windows and sh on Linux how portable would, for example,
be?
More portable, since on my windows system at least, there is an ls.
On your Windows system maybe, but not on the vast majority of Windows systems.
So not much more portable at all...
But, in fact, the portability does not depend so much on the existance
of a certain executable.
Of course it does. That's what Runtime.exec() is doing, running an external, OS
specific, executable. If the executable you are trying to run only exists on
one OS, or if the argument semantics are different, then the call will fail on
other OSs.
Even if ls is not accessible for some reason (which can happen under
UNIX/LINUX also for other reasons than non-existance), the java
environment still could provide the following abstraction: Execute a
command string in such a way that it works as if one had typed the
command at the command line.
Which command line? My Linux has at least half a dozen different shells to
choose from - e.g. sh,ksh,csh,tcsh,bash all of which are subtely different.
Since there is a lowest common denominator between a unix shell and
cmd.exe about quoting, wildcards, etc. one could go a fairly long way.
Not really. Both quoting and globbing are different between cmd.exe and
UNIX/Linux shells. There are also very many other differences.
Look, I am not saying anything against Runtime.exec(String[]). But the
"convenience" method Runtime.exec(String) is almost useless (since it
does tokenization,
Yes, I agree.
but does not provide for escape mechanism and
ignores quotation). It would be much more convenient (and intuitive,
and simple) to let Runtime.exec(cmdstr) be equivalent to
Runtime.exec(new String[] {"cmd.exe", "/c", cmdstr}); // Win
Runtime.exec(new String[] {"sh", "-c", cmdstr}); // Unix
.... // etc.
The appropriate command interpreter for each os could be found through
a system property.
and there would be no guarantee that the chosen command interpreter would
actually interpret the string in the way the programmer hoped. The only way to
be sure, and programming is about certainty, is to explicitly execute the
correct shell.
I am curious how many java programs contain incomplete, bug ridden
implementations of basic shell features like tokenization, quotation,
wildcard matching, etc. in order to enhance the useless
Runtime.exec(String).
That's why when using Runtime.exec() you should invoke the correct executable,
whether that be a shell/command interpreter or the direct executable, so that
you are not guessing how the these "basic shell features" might get
interpreted.
--
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
.
- References:
- Executing command with Runtime.getRuntime.exec() fails
- From: Lionel
- Re: Executing command with Runtime.getRuntime.exec() fails
- From: Gordon Beaton
- Re: Executing command with Runtime.getRuntime.exec() fails
- From: Lionel
- Re: Executing command with Runtime.getRuntime.exec() fails
- From: Ingo Menger
- Re: Executing command with Runtime.getRuntime.exec() fails
- From: Nigel Wade
- Re: Executing command with Runtime.getRuntime.exec() fails
- From: Ingo Menger
- Re: Executing command with Runtime.getRuntime.exec() fails
- From: Nigel Wade
- Re: Executing command with Runtime.getRuntime.exec() fails
- From: Ingo Menger
- Executing command with Runtime.getRuntime.exec() fails
- Prev by Date: Re: Unchecked Cast
- Next by Date: Re: Great SWT Program
- Previous by thread: Re: Executing command with Runtime.getRuntime.exec() fails
- Next by thread: Re: Executing command with Runtime.getRuntime.exec() fails
- Index(es):
Relevant Pages
|