Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在shell中获取java输出_Java_Bash_Shell - Fatal编程技术网

如何在shell中获取java输出

如何在shell中获取java输出,java,bash,shell,Java,Bash,Shell,我有一个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, InterruptedExcep

我有一个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);
            }

        }
在my 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中处理该文件。或者您可以通过管道(|)输出。

您可以将jar的输出重定向到一个文件(>out.txt,在run.sh中$3之后),并在run.sh中处理该文件。或者您可以通过管道(|)输出。

不仅仅是
“$JAVA_HOME”/bin/JAVA-jar test.jar$1$2$3>test.txt
工作吗?问题是其中一个输出参数有1000行文件路径。从文本文件中写入和读取可能会增加计算时间。你认为呢?从文件中读写要比在控制台中读快。试试看。不就是
“$JAVA_HOME”/bin/JAVA-jar test.jar$1$2$3>test.txt就行了吗?问题是其中一个输出参数是1000行文件路径。从文本文件中写入和读取可能会增加计算时间。你认为呢?从文件中读写要比在控制台中读快。试试看。问题是其中一个输出参数有1000行文件路径。从文本文件中写入和读取可能会增加计算时间。这里我可能错了。System.out.print(arg1,“|”和“arg2”);然后使用shell脚本来解析它?不,在$3之后的run.sh中,您可以通过管道将输出传输到shell命令,而不是重定向。问题是其中一个输出参数有1000行文件路径。从文本文件中写入和读取可能会增加计算时间。这里我可能错了。System.out.print(arg1,“|”和“arg2”);然后使用shell脚本来解析它?不,在$3之后的run.sh中,您可以通过管道将输出传输到shell命令,而不是重定向。