【发布时间】:2016-01-09 16:42:46
【问题描述】:
我正在尝试运行一个 OSX 命令,它是 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<String> output = new LinkedList<String>();
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"})
我也试过了,
- 在命令字符串中加入撇号
- 按照我的几个站点的建议更改数组字符串中的命令
但没有一个有效。
如果我在安排我的命令时做错了什么或我犯了任何语法错误,请告知。提前致谢。
【问题讨论】:
-
可能不是这里真正发生的事情,但也许是“教程”拼写错误的事实?也许文件夹拼写不同。
-
没有问题是空间使计算机成为两条不同的路径。由于这些路径不存在,因此会出现错误。
-
嗨@Arc676,当我问这个问题时,我有点改变了命名,可能遗漏了一些东西,但这不是问题所在..哈哈
-
嗨@RealSkeptic!!!!效果很好!!!