yesh
yesh

Reputation: 2070

How to get java output in shell

I have a shell script run.sh which is executing a jar file.

#!/bin/bash
"$JAVA_HOME"/bin/java -jar test.jar $1 $2 $3

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.

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.

#!/bin/bash

IMAGE=$1
ROW=$2
COLUMN=$3
DESTINATION=$4
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?

Upvotes: 1

Views: 7013

Answers (1)

dan
dan

Reputation: 13272

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.

Upvotes: 1

Related Questions