-3

I use this code to execute a script within my java program

pb.environment().put("time", time);
pb.environment().put("value", value);
Process p = pb.start();     
p.waitFor();  
// get the return value

After waiting for the process is it possible to get the output value ? With output value I mean the echo values used in the script

1

1 Answer 1

0

try something like this;

test.sh

#!/bin/bash
echo $time
echo $value

java class;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class RunShellScript {
    public static void main(String[] args) {

        String command = "/tmp/test.sh";
        try {
            ProcessBuilder pb = new ProcessBuilder(command);

            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            String time = sdf.format(cal.getTime());
            String value = "your Value";


            pb.environment().put("time", time);
            pb.environment().put("value", value);

            Process p = pb.start();
            p.waitFor();  

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new InputStreamReader(
                    p.getErrorStream()));

            // to standard output
            String s = null;
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // to standard error
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

java console output:

16:03:03
your Value
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.