Runtime.exec(String[]) does not correctly handle a command argument that
contains a space and ends with a backslash.
-- The following program demonstrates the problem
import java.io.*;
import java.util.*;
public class Cmd {
private static String getJavaCommand() {
String javaHome = System.getProperty("java.home");
if (javaHome != null && javaHome.length() > 0)
return (javaHome
+ File.separatorChar + "bin"
+ File.separatorChar + "java");
else
return "java";
}
public static void main(String[] args) throws Exception {
if (args.length > 0) {
System.err.println("child: " + args[0]);
return;
}
String[] cmd = new String[3];
cmd[0] = getJavaCommand();
cmd[1] = "Cmd";
cmd[2] = "foo bar\\baz\\";
System.err.println("parent: " + cmd[2]);
Process process = Runtime.getRuntime().exec(cmd);
InputStream in = process.getErrorStream();
byte[] buf = new byte[1024];
int n;
while ((n = in.read(buf)) >= 0)
System.out.write(buf, 0, n);
}
}
-- Expected output
parent: foo bar\bazchild: foo bar\baz
-- Actual output
parent: foo bar\bazchild: foo bar\baz"