[英]Cannot launch shell script with arguments using Java ProcessBuilder
我正在嘗試使用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", "<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
#!/bin/bash
sh <path to script>/clearRegressionResult.sh (This runs fine)
sh <path to script>/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所以也許你找不到普通文件? 我可能錯了,希望它有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.