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



Ingo Menger wrote:

On 29 Nov., 18:05, Nigel Wade <n...@xxxxxxxxxxxx> wrote:
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,
Runtime.exec("ls")
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
.



Relevant Pages

  • Re: Running Ring3 command from Ring0 in Windows?
    ... I am looking for a way to execute Ring3 command (for ex, ... The motivation of this is that I can exploit Windows kernel, ... executing in Ring0 only, and want to run some code in Ring3, too. ...
    (Pen-Test)
  • Re: working w/ windows-only CDs in Virtual; PC 6.1
    ... I think if you hold down the fn key and press F10 then the ... > sometimes interactive CD's and they are typicaly windows only. ... > HAVE to be able to execute this one command on this one CD. ...
    (microsoft.public.mac.otherproducts)
  • MS04-012 breaks AT command when managing NT 4.0 scheduler from XP PC
    ... Since installing the patch for MS04-012 on my Windows XP machine, ... I get the following error when using the AT command to manage scheduled jobs ... Another workaround is to execute the AT command from the remote server (or ... I was interested to notice that the three DLLs in question, ...
    (NT-Bugtraq)
  • [Full-Disclosure] Advanced usage of system() function.
    ... and call its arguments as a command for shell. ... as we can see we still didnt get what we want (typing exit ... Connection closed by foreign host. ... think what we want to execute. ...
    (Full-Disclosure)
  • RE: re-setting boot partition
    ... Description of the Windows 2000 Recovery Console ... For a Microsoft Windows XP version of this article, ... MB of hard disk space on your system partition to hold the Cmdcons folder ... Windows NTBoot Console Command Interpreter. ...
    (microsoft.public.win2000.setup_upgrade)