简体   繁体   English

无法通过Runtime.exec运行命令

[英]Not able to run command by Runtime.exec

Below command I can run though terminal but when I am trying to execute it through java code its giving me 127 exit code. 下面的命令我可以通过终端运行,但是当我尝试通过Java代码执行它时,它会给我127退出代码。

sledge connect --cluster_id=<name> --namespace=<name> --password=<pass>

Java: Java:

   try {
            String[] cmd = { "/bin/bash", "sledge connect --cluster_id=<name> --namespace=<name> --password=<pass>"};
            Process p = Runtime.getRuntime().exec(cmd);
            int po = p.waitFor();
            System.out.println(po);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

You'd need to add "-c" in your commands array, so replace : 您需要在命令数组中添加“ -c”,因此请替换:

String[] cmd = { "/bin/bash", "sledge connect --cluster_id=<name> --namespace=<name> --password=<pass>"};

With : 与:

String[] cmd = { "/bin/bash", "-c", "sledge connect --cluster_id=<name> --namespace=<name> --password=<pass>"};

From bash manual : 从bash手册:

-c string If the -c option is present, then commands are read from string. -c字符串如果存在-c选项,则从字符串读取命令。 If there are arguments after the string, they are assigned to the positional parame‐ ters, starting with $0. 如果字符串后面有参数,则将其分配给位置参数,从$ 0开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM