简体   繁体   中英

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.

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

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 :

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 :

-c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parame‐ ters, starting with $0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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