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

标签 java macos shell

我正在尝试运行一个 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"})

我也尝试过,

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

但没有一个起作用。

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

最佳答案

调用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

关于路径名称上带有空格的 Java Runtime Exec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33077129/

相关文章:

java - Jersey 3 - 使用bindFactory配置绑定(bind)

Java缓存对象返回旧值

python - 为什么 tkinter 按钮的高度选项在 OSX 和 Windows 上的行为不同?

shell - 将 jenkins 管道中设置的变量传递给 shell 脚本

linux - 检查 wget/curl 是否存在

java - 如何通过 Swing 加快图像的放大/缩小速度?

java - java学习者培训中随机删除文本文件

objective-c - 自毁 cocoa 应用

macos - minishift 上 openshift 客户端的权限问题

shell - 将 Stderr 发送到 dev/null 但如果没有发生错误,则继续