我有一個shell腳本run.sh它正在執行一個jar文件。如何在shell中獲取java輸出
#!/bin/bash
"$JAVA_HOME"/bin/java -jar test.jar $1 $2 $3
在完成java中的所有過程之後,我需要執行一個命令。所以我的java方法通過傳遞一些參數來調用一個 shell腳本。
public static void Test(String Arg1, int Arg2, int Arg3, String Arg4) throws IOException, InterruptedException {
File currentLocation = new File(Test.class.getProtectionDomain().getCodeSource().getLocation().getPath());
String executeShellScript = currentLocation.getParentFile().toString() + "/out.sh";
//SOME LOGIC
ProcessBuilder pb = new ProcessBuilder("/bin/sh",executeShellScript,arg1, arg2, arg3, arg4);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
而在我的out.sh中,我接受參數並執行命令。
#!/bin/bash
IMAGE=$1
ROW=$2
COLUMN=$3
DESTINATION=$4
echo $IMAGE
我在想,如果我能執行並讓所有的參數處理我的罐子從相同的腳本(run.sh)輸出?
不會只是'「$ JAVA_HOME」/ bin/java -jar test.jar $ 1 $ 2 $ 3> test.txt'工作嗎? –
問題是其中一個輸出參數是幾千行文件路徑。從文本文件寫入並讀取它可能會增加計算時間。你怎麼看 ? –
從文件中寫入和讀取比在控制檯中執行要快。嘗試一下。 –