0

I have 2 scenarios below. One where I use the Runtime.getRuntime().exec method with arguments this does not seems to work I get the error while running the bash script.

However if i use the exec method without passing any values to the exec parameters it works.

String cfenv_location="bash /root/.cfenv/environments/ussouth_ys1/bin/cf ";
Process p = Runtime.getRuntime().exec(new String[]{cfenv_location, "create-service", servicename, planname, appname.replaceAll(" ", "-")});

Error:

exception happened - here's what I know:
java.io.IOException: Cannot run program "bash /root/.cfenv/environments/ussouth_ys1/bin/cf": error=2, No such file or directory
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
        at java.lang.Runtime.exec(Runtime.java:617)
        at java.lang.Runtime.exec(Runtime.java:485)
        at JavaRunCommand.cfCreateService(JavaRunCommand.java:111)
        at JavaRunCommand.main(JavaRunCommand.java:359)
Caused by: java.io.IOException: error=2, No such file or directory
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
        at java.lang.ProcessImpl.start(ProcessImpl.java:130)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
        ... 4 more

Works

String cfenv_location="bash /root/.cfenv/environments/ussouth_ys1/bin/cf ";
String command = cfenv_location+"login -a "+urllocation+ " -u "+username+ " -p "+password+ " -o "+org+" -s "+space;
Process p = Runtime.getRuntime().exec(command);
10
  • Can you try to remove the trailing space in first example ? Or even better I'd suggest to have bash as command and the rest as parameters... Commented Feb 13, 2015 at 20:37
  • I did try that but that does not seem to solve the problem. I get the same error Commented Feb 13, 2015 at 20:38
  • You tried also the second option? Commented Feb 13, 2015 at 20:39
  • 2
    Yes, exactly that, and you removed starting "bash " from the cfenv_location variable, right? Please update the question with all you tried (and error messages), to prevent others to ask the same... Commented Feb 13, 2015 at 20:48
  • 1
    @ShekharSuman: come on, title is not for tracking state! Commented Feb 13, 2015 at 21:07

1 Answer 1

1

Message:

Cannot run program "bash /root/.cfenv/environments/ussouth_ys1/bin/cf"

shows, that this String is interpreted as a command instead of command and argument (separated with space character).

The proper way is to split the string and call it as

Runtime.getRuntime().exec(new String[]{"bash", cfenv_location, ...

where cfenv_location is without starting "base " part...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.