Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!news.dfncis.de!not-for-mail From: =?ISO-8859-1?Q?Sven_K=F6hler?= Newsgroups: comp.lang.java.programmer Subject: Re: exec problem is JDK 1.7.0_21 Date: Tue, 23 Apr 2013 23:17:02 +0300 Lines: 67 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: news.dfncis.de ewiVD0Ha2r0U0hzBkS9W7Q6sQK/9hCPRU9YHLOBv7wK3P7Xi4xwx1dTi5k Cancel-Lock: sha1:nMcH04GYl4IC9Qi9V0+ET0ElLSE= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130408 Thunderbird/17.0.5 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:23597 Am 23.04.2013 12:48, schrieb Steven Simpson: > On 20/04/13 22:40, Sven Köhler wrote: >> Oh, and of course the ProcessBuilder doesn't behave as it is supposed >> to. As mentioned in my first post, the String "\"a b\"" would be passed >> unmodified to the program invoked. However, clearly, the string passed >> to the program should have been >> "\"\\\"a b\\\"\"" >> >> Only with the quotes and backslashes added, CommandLineToArgv would >> decode it to "\"a b\"". With the current ProcessBuilder implementation, >> a Windows program will see the parameter "a b" while on UNIX the program >> will see "\"a b\"". >> >> See >> http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391%28v=vs.85%29.aspx >> >> for details. > > You're expecting Java to build its Windows command string something like > this?: > ...code.. > > Does that work correctly for anything to be thrown at CommandLineToArgvW? I haven't checked your code, but I have written the inverse to CommandLineToArgv in Java. It's not that hard. Passing the result to ProcessBuilder works fine - even though that it works is based on the two silly undocumented facts: (1) ProcessBuilder adds quotes if and only if the argument doesn't start and end with quotes and (2) ProcessBuilder doesn't mess with quotes and backslashes inside the arguments. The problem with Java's ProcessBuilder is, that people use it "willy nilly" being completely oblivous about the fact that it doesn't do what might be best for them. (Recall the case "c:\\program files\\", "world" where the two strings are basically merged to "c:\\program files\" world". This is probably the scariest.) Secondly, there is no way to pass the arguments correctly by only relying on documented stuff. >> Note, that I assume that the program invoked uses CommandLineToArgv to >> decode the command line. Which is by no means clear, as any program can >> implement their own tokenizer. >> >> Don't you find that a bit strange? > > Perhaps that assumption is too risky for Java to make, e.g. there are > enough 'native' Windows/DOS commands around that the programmer is > likely to want to invoke, but don't use CommandLineToArgvW, and so would > be confused if they received a string escaped as above. Not a very > satisfactory situation. Some assumption about the program's tokenizer are already part of ProcessBuilder's implementation (namely that they understand quotes around arguments with spaces). Isn't that risky already? It still puzzles me: The documentation explicitly mentions operating systems, where programs perform the tokenization themselves. It even suggests that the String-list should exist of exactly two elements (which could maybe interpretated as a hint that the second element could be the raw command line parameter string). But instead, what we get, is an imperfect, error prone, undocumented mangling of arguments: adding quotes without taking care of the quotes within the strings is something, that shouldn't slip any programmer's attention. Regards, Sven