Re: exec problem is JDK 1.7.0_21
On Tue, 23 Apr 2013 23:28:35 +0300, Sven K??hler wrote:
Am 23.04.2013 00:07, schrieb Martin Gregorie:
My testprog works exactly the same when run from within my
TestProcessBuilder test class as it does when run stand-alone from the
command line:
$ java TestProcessBuilder testprog "hello world" "\"hello world\""
"\"hello\" \"world\"" "hello ""double quoted"" world"
argc=5 argv[0]=testprog argv[1]=hello world argv[2]="hello world"
argv[3]="hello" "world"
Please look at the results that Steven posted. If the String "hello\"
\"world" is passed to the ProcessBuilder, the result was:
argv[1]=[hello] argv[2]=[world]
Actually, on Linux almost all the messing about with strings is done by
the shell that invokes the the command if the strings are enclosed in
double quotes. Use single quotes instead and the strings arrive at the
program ProcessBuilder is running with no modification at all. Here are
the results of using (1) double quoted strings and (2) single quoted
strings.
$ java TestProcessBuilder testprog "hello world" "\"hello world\""
"\"hello\" \"world\"" "hello ""double quoted"" world"
argc=5
argv[0]=testprog
argv[1]=hello world
argv[2]="hello world"
argv[3]="hello" "world"
argv[4]=hello double quoted world
exitValue=5
$ java TestProcessBuilder testprog 'hello world' '\"hello world\"'
'\"hello\" \"world\"' 'hello ""double quoted"" world'
argc=5
argv[0]=testprog
argv[1]=hello world
argv[2]=\"hello world\"
argv[3]=\"hello\" \"world\"
argv[4]=hello ""double quoted"" world
exitValue=5
using the same TestProcessBuilder.java program as I used before. Note
that the *second* example is what will happen if the arguments are read
from an external source such as a file but, of course, if the arguments
are program literals embedded in the source text then the internal double
quotes inside them must be escaped like this:
====================== TestProcessBuilder2.java =======================
import java.io.*;
public class TestProcessBuilder2
{
public static void main(String[] args) throws Exception
{
String[] argList = { "testprog",
"hello world",
"\"hello world\"",
"\"hello\" \"world\"",
"hello \"\"double quoted\"\" world"};
ProcessBuilder pb = new ProcessBuilder(argList);
pb.redirectErrorStream(true);
Process p = pb.start();
Thread t = new Thread(new TestRunnable(p.getInputStream()));
t.start();
t.join();
System.out.println("exitValue=%d" + p.exitValue());
}
}
====================== TestProcessBuilder2.java =======================
$ java TestProcessBuilder2
argc=5
argv[0]=testprog
argv[1]=hello world
argv[2]="hello world"
argv[3]="hello" "world"
argv[4]=hello ""double quoted"" world
exitValue=%d5
$
Oh, and then there was the neat case, of passing the two argument
strings "c:\program files\" and "world" to the ProcessBuilder.
You need to show the exact code line(s) used for this: the exact detail
is critical because what you described above should not compile. Assuming
it is written as:
new ProcessBuilder("c:\program files\", "world");
then the first string is actually "c:\program files\", " because the
second quote was escaped and so looses its meaning as a string
terminator. This leaves the sequence [world"] with an unmatched quote as
its last character.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |