The call Runtime.getRuntime().exec(cmdStr)
is a convenience method - a shortcut for calling the command with an array. It splits the command string on white spaces, and then runs the command with the resulting array.
So if you give it a string in which any of the parameters includes spaces, it does not parse quotes like the shell does, but just breaks it into parts like this:
// Bad array created by automatic tokenization of command string
String[] cmdArr = { "plutil",
"-convert",
"json",
"-o",
"-",
"'/Users/chris/project/temp",
"tutoral/project.plist'" };
Of course, this is not what you want. So in cases like this, you should break the command into your own array. Each parameter should have its own element in the array, and you don't need extra quoting for the space-containing parameters:
// Correct array
String[] cmdArr = { "plutil",
"-convert",
"json",
"-o",
"-",
"/Users/chris/project/temp tutoral/project.plist" };
Note that the preferred way to start a process is to use ProcessBuilder
, e.g.:
p = new ProcessBuilder("plutil",
"-convert",
"json",
"-o",
"-",
"/Users/chris/project/temp tutoral/project.plist")
.start();
ProcessBuilder
offers more possibilities, and using Runtime.exec
is discouraged.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…