[英]Runtime exec method with arguments does not run bash script in Java
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.
我在其中使用带有参数的Runtime.getRuntime()。exec方法的这似乎不起作用,我在运行bash脚本时收到错误。
However if i use the exec method without passing any values to the exec parameters it works. 但是,如果我使用exec方法而不将任何值传递给exec参数,则它会起作用。
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);
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). 显示,此String被解释为命令,而不是命令和参数(用空格字符分隔)。
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... 其中cfenv_location没有开始
"base "
部分...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.