【问题标题】:Java Runtime Exec With White Spaces On Path Name路径名称上带有空格的 Java 运行时 Exec
【发布时间】: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!!!!效果很好!!!

标签: java macos shell


【解决方案1】:

试试这个

/Users/chris/project/temp\ tutoral/project.plist

编辑:我在第一篇文章中放错了强烈反对

【讨论】:

  • @Chris Try temp\\ tutoral
  • 仍然无法正常工作@bmarkham,但 RealSkeptic 给出的答案效果很好!
【解决方案2】:

调用Runtime.getRuntime().exec(cmdStr) 是一种方便的方法 - 使用数组调用命令的快捷方式。它将命令字符串拆分为空格,然后使用结果数组运行命令。

因此,如果你给它一个字符串,其中任何参数都包含空格,它不会像 shell 那样解析引号,而只是将它分成如下部分:

// Bad array created by automatic tokenization of command string
String[] cmdArr = { "plutil",
                    "-convert",
                    "json",
                    "-o",
                    "-",
                    "'/Users/chris/project/temp",
                    "tutoral/project.plist'" };

当然,这不是你想要的。因此,在这种情况下,您应该将命令分解为您自己的数组。每个参数在数组中都应该有自己的元素,包含空格的参数不需要额外引用:

// Correct array 
String[] cmdArr = { "plutil",
                    "-convert",
                    "json",
                    "-o",
                    "-",
                    "/Users/chris/project/temp tutoral/project.plist" };

请注意,启动进程的首选方式是使用ProcessBuilder,例如:

p = new ProcessBuilder("plutil",
                       "-convert",
                       "json",
                       "-o",
                       "-",
                       "/Users/chris/project/temp tutoral/project.plist")
       .start();

ProcessBuilder 提供更多可能性,不鼓励使用Runtime.exec

【讨论】:

  • 感谢@RealSkeptic 的解决方案我不知道我需要分隔每个具有白色间距的参数/参数。
猜你喜欢
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-21
  • 2015-06-23
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
相关资源
最近更新 更多