路径名称上带有空格的Java Runtime Exec [英] Java Runtime Exec With White Spaces On Path Name

查看:83
本文介绍了路径名称上带有空格的Java Runtime Exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to run a OSX command which is plutil to convert certain plist to json format. The command that I use in terminal is

 plutil -convert json -o - '/Users/chris/project/temp tutoral/project.plist'

该命令的路径名带有白色间距,在我的终端中可以很好地运行,并且用单引号()覆盖路径名,但是问题在于在Java的Runtime.getRuntime().exec(cmdStr)中运行此命令是我编写的代码

This command with path name having white spacing works fine in my terminal with the apostrophe (") sign covering the path name but the problem is with running this command in java's Runtime.getRuntime().exec(cmdStr) below is the code that i wrote

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();
        }
    }

如果我运行这段代码,将会给我一个错误

If i run this code it would give me an error of

'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"})

我也尝试过

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

但其中没有一个起作用.

but non of them worked.

如果我在安排命令时遇到任何错误或出现的任何语法错误,请提出建议.预先感谢.

Please advice if i did anything wrong in arranging my command or any syntax error that i made. Thanks in advance.

推荐答案

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

The call Runtime.getRuntime().exec(cmdStr) is a convenience method - a shortcut for calling the command with an array. It splits the command string on white spaces, and then runs the command with the resulting array.

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

So if you give it a string in which any of the parameters includes spaces, it does not parse quotes like the shell does, but just breaks it into parts like this:

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

当然,这不是您想要的.因此,在这种情况下,应将命令分成自己的数组.每个参数在数组中都应该有其自己的元素,并且不需要为包含空格的参数加上引号:

Of course, this is not what you want. So in cases like this, you should break the command into your own array. Each parameter should have its own element in the array, and you don't need extra quoting for the space-containing parameters:

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

请注意,启动过程的首选方法是使用

Note that the preferred way to start a process is to use ProcessBuilder, e.g.:

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

ProcessBuilder提供了更多的可能性,不建议使用Runtime.exec.

ProcessBuilder offers more possibilities, and using Runtime.exec is discouraged.

这篇关于路径名称上带有空格的Java Runtime Exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆