bash 如何在shell中获取java输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12819154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get java output in shell
提问by yesh
I have a shell script run.shwhich is executing a jar file.
我有一个 shell 脚本run.sh,它正在执行一个 jar 文件。
#!/bin/bash
"$JAVA_HOME"/bin/java -jar test.jar
After doing all the process in java I need to execute a command. So my java method calls a shell script by passing it some arguments.
在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);
}
}
And in my out.sh I take in the arguments and execute a command.
在我的 out.sh 中,我接收参数并执行命令。
#!/bin/bash
IMAGE=
ROW=
COLUMN=
DESTINATION=
echo $IMAGE
I was wondering if I could execute and process the output of my jar from the same script (run.sh) by getting all the arguments?
我想知道是否可以通过获取所有参数来执行和处理来自同一个脚本 (run.sh) 的 jar 的输出?
采纳答案by dan
You can redirect the output of your jar in a file (>out.txt after your $3 in run.sh) and process that file in run.sh. or you can pipe (|) the output.
您可以将 jar 的输出重定向到一个文件(run.sh 中的 $3 之后的 >out.txt)并在 run.sh 中处理该文件。或者您可以通过管道 (|) 输出。

