我有一个 shell 脚本 run.sh ,它正在执行一个 jar 文件。
#!/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)的 jar 的输出?
最佳答案
您可以将 jar 的输出重定向到文件中(>out.txt 在 run.sh 中的 $3 之后)并在 run.sh 中处理该文件。或者您可以通过管道 (|) 输出。
关于java - 如何在shell中获取java输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12819154/