我正在尝试运行一个OSPL命令,该命令是plutil,用于将某些plist转换为json格式。我在终端中使用的命令是
plutil -convert json -o - '/Users/chris/project/temp tutoral/project.plist'
路径名称带有白色间距的此命令在我的终端中可以很好地运行,并且我的终端用单引号(“)覆盖路径名,但是问题在于在Java
Runtime.getRuntime().exec(cmdStr)下面运行此命令是我编写的代码
public static void main(String args[]){
LinkedList output = new LinkedList();
String cmdStr = "plutil -convert json -o - /Users/chris/project/temp tutoral/project.plist";
//String cmdStr = " plutil -convert json -o - '/Users/chris/project/temp tutoral/project.plist'";
//String [] cmdStr ={ "plutil -convert json -o - ", "\"Users/chris/project/temp tutoral/project.plist\""};
Process p;
try {
p = Runtime.getRuntime().exec(cmdStr);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.add(line);
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
如果我运行此代码,它将给我一个错误
'Users/chris/project/temp: file does not exist or is not readable or is not a regular file (Error Domain=NSCocoaErrorDomain Code=260 "The file “temp” couldn’t be opened because there is no such file." UserInfo=0x7fd6b1c01510 {NSFilePath='Users/chris/project/temp, NSUnderlyingError=0x7fd6b1c01280 "The operation couldn’t be completed. No such file or directory"})
tutoral/project.plist': file does not exist or is not readable or is not a regular file (Error Domain=NSCocoaErrorDomain Code=260 "The file “project.plist” couldn’t be opened because there is no such file." UserInfo=0x7fd6b1d6dd00 {NSFilePath=tutoral/project.plist', NSUnderlyingError=0x7fd6b1d6c6b0 "The operation couldn’t be completed. No such file or directory"})
我也尝试过
在命令字符串中放入撇号
按我建议的几个站点更改数组字符串中的命令
但没有一个成功。
如果我在安排命令时发生任何错误或任何语法错误,请提出建议。提前致谢。