gpt4 book ai didi

java - 带参数的运行时 exec 方法不会在 Java 中运行 bash 脚本

转载 作者:行者123 更新时间:2023-12-01 11:55:14 25 4
gpt4 key购买 nike

我有以下两种情况。当我使用带有参数的 Runtime.getRuntime().exec 方法时,这似乎不起作用,我在运行 bash 脚本时收到错误。

但是,如果我使用 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(" ", "-")});

错误:

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

作品

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

最佳答案

消息:

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

表明,该字符串被解释为命令,而不是命令和参数(用空格字符分隔)。

正确的方法是分割字符串并将其称为

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

其中 cfenv_location 没有开始 "base " 部分...

关于java - 带参数的运行时 exec 方法不会在 Java 中运行 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28508284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com