无法使用 Java ProcessBuilder 启动带参数的 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18240944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot launch shell script with arguments using Java ProcessBuilder
提问by DigitalDyn
I am trying to execute a shell script with command line arguments using ProcessBuilder, this shell script inturn calls two other shell scripts that uses this argument. The first shell script runs fine, but when the second one is started it returns exit code 1.
我正在尝试使用 ProcessBuilder 执行带有命令行参数的 shell 脚本,这个 shell 脚本反过来调用另外两个使用此参数的 shell 脚本。第一个 shell 脚本运行良好,但是当第二个脚本启动时,它返回退出代码 1。
ProcessBuilder snippet from Java Program:
Java 程序中的 ProcessBuilder 片段:
//scenario - A string that holds a numerical value like 1 or 2 etc
String[] command2 = {"/bin/bash", "<path to shell script>/runTemporaryTestSuite.sh", scenario};
ProcessBuilder pb2 = new ProcessBuilder(command2);
Process p2 = pb2.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p2.getInputStream()));
String line;
//print - is an object ref of response.getWriter() //
print.println("Output of running "+Arrays.toString(command2)+" is: ");
while ((line = br.readLine()) != null) {
print.println(line);
}
try {
int exitValue = p2.waitFor();
print.println("<br><br>Exit Value of p2 is " + exitValue);
} catch (InterruptedException e) {
e.printStackTrace();
}
runTemporaryTestSuite.sh
运行TemporaryTestSuite.sh
#!/bin/bash
sh <path to script>/clearRegressionResult.sh (This runs fine)
sh <path to script>/startRegression.sh (This is where the issue occurs)
startRegression.sh looks like:
startRegression.sh 看起来像:
SUITE_PATH="./"
java -DconfigPath=${SUITE_PATH}/config.xml -Dscenario= -Dauto=true -jar test.jar
My output: Output of running [/bin/bash, /runTemporaryTestSuite.sh, 29] is: Exit Value of p2 is 1
我的输出:运行 [/bin/bash, /runTemporaryTestSuite.sh, 29] 的输出是: p2 的退出值为 1
Any help in resolving this is really appreciated.
任何解决此问题的帮助都非常感谢。
采纳答案by drgn
In think the problem is not that you cannot launch shell script with arguments, I was curious and I did a test
我认为问题不在于你不能用参数启动 shell 脚本,我很好奇,我做了一个测试
public class Main {
public static void main(String[] args) throws IOException {
String[] command = {"/bin/bash", "test.sh", "Argument1"};
ProcessBuilder p = new ProcessBuilder(command);
Process p2 = p.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p2.getInputStream()));
String line;
System.out.println("Output of running " + command + " is: ");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
here is the test.sh script
这是 test.sh 脚本
echo Hello im the script, here your args $@
Here the output
这里的输出
Output of running [Ljava.lang.String;@604e9f7f is:
Hello im the script, here your args Argument1
What I think is just that your startRegression.sh exit with a non-0 status (aka it failed somewhere) and it have repercussion, runTemporaryTestSuite.sh will also exit with a non-zero status, and so on hence the message : Exit Value of p2 is 1
我认为只是你的 startRegression.sh 以非 0 状态退出(也就是它在某处失败)并且它产生了影响,runTemporaryTestSuite.sh 也会以非零状态退出,等等因此消息:退出值p2 的值为 1
What I see right now,
我现在看到的,
SUITE_PATH="./" java -DconfigPath=${SUITE_PATH}/config.xml [..] the configPath will be .//config.xml so maybe you have a plain file not found issue? I might be wrong, hope it helped
SUITE_PATH="./" java -DconfigPath=${SUITE_PATH}/config.xml [..] configPath 将是 .//config.xml 所以也许你有一个未找到的纯文件问题?我可能错了,希望它有帮助