路径名上带有空格的 Java Runtime Exec

Chr*_*ris 2 java macos shell

我正在尝试运行一个 OSX 命令,它是 plutil 将某些 plist 转换为 json 格式。我在终端中使用的命令是

 plutil -convert json -o - '/Users/chris/project/temp tutoral/project.plist'
Run Code Online (Sandbox Code Playgroud)

这个路径名有空格的命令在我的终端中工作正常,撇号 (") 符号覆盖了路径名,但问题是在Runtime.getRuntime().exec(cmdStr)下面的java 中运行这个命令是我写的代码

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果我运行此代码,它会给我一个错误

'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"})
Run Code Online (Sandbox Code Playgroud)

我也试过,

  • 在命令字符串中放入撇号
  • 按照我的几个站点的建议更改数组字符串中的命令

但他们都没有工作。

如果我在安排我的命令或我犯的任何语法错误时做错了什么,请提出建议。提前致谢。

Rea*_*tic 6

调用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'" };
Run Code Online (Sandbox Code Playgroud)

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

// Correct array 
String[] cmdArr = { "plutil",
                    "-convert",
                    "json",
                    "-o",
                    "-",
                    "/Users/chris/project/temp tutoral/project.plist" };
Run Code Online (Sandbox Code Playgroud)

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

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

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