2012-10-10 140 views
0

我有一個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)輸出?

+0

不會只是'「$ JAVA_HOME」/ bin/java -jar test.jar $ 1 $ 2 $ 3> test.txt'工作嗎? –

+0

問題是其中一個輸出參數是幾千行文件路徑。從文本文件寫入並讀取它可能會增加計算時間。你怎麼看 ? –

+1

從文件中寫入和讀取比在控制檯中執行要快。嘗試一下。 –

回答

1

您可以將jar的輸出重定向到一個文件(run.sh中的$ 3之後的out.txt)並在run.sh中處理該文件。或者你可以輸出(|)輸出。

+0

問題是其中一個輸出參數是幾行1000行的文件路徑。從文本文件寫入並讀取它可能會增加計算時間。我可能在這裏錯了。 –

+0

您也可以輸出輸出。 – dan

+0

System.out.print(arg1,「|」,「arg2」); 並使用shell腳本來解析這個? –