无法使用Java ProcessBuilder启动带有参数的shell脚本

我正在尝试使用ProcessBuilder使用命令行参数执行shell脚本,此shell脚本调用另外两个使用此参数的shell脚本。 第一个shell脚本运行正常,但是当第二个脚本启动时它返回退出代码1。

Java程序的ProcessBuilder片段:

//scenario - A string that holds a numerical value like 1 or 2 etc String[] command2 = {"/bin/bash", "/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("

Exit Value of p2 is " + exitValue); } catch (InterruptedException e) { e.printStackTrace(); }

runTemporaryTestSuite.sh

 #!/bin/bash sh /clearRegressionResult.sh (This runs fine) sh /startRegression.sh $1 (This is where the issue occurs) 

startRegression.sh看起来像:

 SUITE_PATH="./" java -DconfigPath=${SUITE_PATH}/config.xml -Dscenario=$1 -Dauto=true -jar test.jar 

我的输出:运行[/ bin / bash,/ runTemporaryTestSuite.sh,29]的输出是:p2的退出值是1

任何解决这个问题的帮助都非常感谢。

认为问题不是你不能用参数启动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); } } 

这是test.sh脚本

 echo Hello im the script, here your args $@ 

这里输出

 Output of running [Ljava.lang.String;@604e9f7f is: Hello im the script, here your args Argument1 

我认为只是你的startRegression.sh退出非0状态(也就是说它失败了)并且它有反响,runTemporaryTestSuite.sh也会以非零状态退出,依此类推消息:退出值p2是1

我现在看到的,

SUITE_PATH =“./”java -DconfigPath = $ {SUITE_PATH} /config.xml [..] configPath将是.//config.xml所以你可能有一个普通文件找不到问题? 我可能错了,希望它有所帮助