Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.fsmpi.rwth-aachen.de!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail From: Steven Simpson Newsgroups: comp.lang.java.programmer Subject: Re: exec problem is JDK 1.7.0_21 Date: Mon, 22 Apr 2013 10:39:19 +0100 Organization: A noiseless patient Spider Lines: 148 Message-ID: <73df4a-rc3.ln1@s.simpson148.btinternet.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: mx05.eternal-september.org; posting-host="a7de32cecf602fe0cc192c8992f5b0c2"; logging-data="28465"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX193lGT/DQcUIRpvHdCAVKJsUQxpGBwtLPU=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130329 Thunderbird/17.0.5 In-Reply-To: Cancel-Lock: sha1:cCODy0FRY92neYYZqIXDmN3KTk4= Xref: csiph.com comp.lang.java.programmer:23566 On 22/04/13 07:36, Sven Köhler wrote: > Am 22.04.2013 04:49, schrieb Knute Johnson: >> On 4/21/2013 1:29 PM, Sven Köhler wrote: >>> Am 21.04.2013 19:20, schrieb Knute Johnson: >>>> One quoted argument "hello world" >>> new ProcessBuilder("test.exe", "hello world"); >>> new ProcessBuilder("test.exe", "\"hello world\""); >>> new ProcessBuilder("test.exe", "\"hello\" \"world\""); >>> >>> Run that on Windows as well as on something UNIX like - e.g. Linux. > The output should have been: > > 1) argv[1]=hello world > 2) argv[1]=hello world > 3) argv[1]=hello > argv[2]=world > > And in the fourth example I added, > new ProcessBuilder("test.exe", "\"\\\"hello world\\\"\""); > the output should have been > > 4) argv[1]="hello world" I tested by cross-compiling a C program 'showargs' from Linux (with mingw32): #include int main(int argc, char **argv) { int i; printf("Arg count: %d\n", argc); for (i = 0; i < argc; i++) printf(" argv[%d]=[%s]\n", i, argv[i]); return 0; } To invoke from Java, I used: import java.io.*; import java.util.*; public class TestProcessBuilder { public static void main(String[] args) throws Exception { test("showargs", "hello world"); test("showargs", "\"hello world\""); test("showargs", "\"hello\" \"world\""); test("showargs", "\"\\\"hello world\\\"\""); } private static void test(String... args) throws IOException, InterruptedException { System.out.println("Java: " + Arrays.asList(args)); ProcessBuilder pb = new ProcessBuilder(args); pb.redirectErrorStream(true); final Process proc = pb.start(); Thread t = new Thread() { public void run() { try (InputStream in = proc.getInputStream()) { byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > -1) { System.out.write(buf, 0, len); } } catch (IOException ex) { System.err.println("error: " + ex); } } }; t.start(); proc.getOutputStream().close(); int rc = proc.waitFor(); t.join(); } } (I compiled this on Linux.) On a Windows VM, I got this: E:\>java -cp . TestProcessBuilder Java: [showargs, hello world] Arg count: 2 argv[0]=[showargs] argv[1]=[hello world] Java: [showargs, "hello world"] Arg count: 2 argv[0]=[showargs] argv[1]=[hello world] Java: [showargs, "hello" "world"] Arg count: 3 argv[0]=[showargs] argv[1]=[hello] argv[2]=[world] Java: [showargs, "\"hello world\""] Arg count: 2 argv[0]=[showargs] argv[1]=["hello world"] E:\> 'java -version' reports: java version "1.7.0_21" Java(TM) SE Runtime Environment (build 1.7.0_21-b11) Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode) On Linux, I got this: $ java -cp . TestProcessBuilder Java: [showargs, hello world] Arg count: 2 argv[0]=[showargs] argv[1]=[hello world] Java: [showargs, "hello world"] Arg count: 2 argv[0]=[showargs] argv[1]=["hello world"] Java: [showargs, "hello" "world"] Arg count: 2 argv[0]=[showargs] argv[1]=["hello" "world"] Java: [showargs, "\"hello world\""] Arg count: 2 argv[0]=[showargs] argv[1]=["\"hello world\""] $ 'java -version' report: java version "1.7.0_15" OpenJDK Runtime Environment (IcedTea7 2.3.7) (7u15-2.3.7-0ubuntu1~12.04.1) OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode) -- ss at comp dot lancs dot ac dot uk