Department of Mathematics and Computer Science

www.univ-soukahras.dz/en/dept/mi

Module: Ingénierie logiciel pour entreprise

  1. Information
  2. Questions

How to receive an output from a process into a Java process as an input via piping ?

3 votes

How to receive an output from a process into a Java process as an input via piping ?

Asked on 16:12, Tuesday 18 Oct 2016 By Imed BOUCHRIKA
In Ingénierie logiciel pour entreprise


answers (1)




Answer (1)

0 votes

static ProcessBuilder.Redirect INHERIT Indicates that subprocess I/O source or destination will be the same as those of the current process.

static ProcessBuilder.Redirect PIPE Indicates that subprocess I/O will be connected to the current Java process over a pipe.

So I don't think one of these will help you redirecting the output of one process to the input of another process.

So let's make our hands dirty and write some code. The relevant line is

RedirectStreams redirectStreams = new RedirectStreams(process1,process2);

public class ProcessPipeTest {
@Test public void testPipe(){
    try {

        Process process1 = new ProcessBuilder("/bin/bash").start();
        Process process2 = new ProcessBuilder("/bin/bash").start();

        RedirectStreams redirectStreams = new RedirectStreams(process1,process2);


        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(process1.getOutputStream()));
        String command = "echo echo echo";
        System.out.println("Input process1: " + command);
        bufferedWriter.write(command + '\n');
        bufferedWriter.close();


        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process2.getInputStream()));
        String actualOutput = bufferedReader.readLine();
        System.out.println("Output process2: " + actualOutput);
        assertEquals("echo",actualOutput);

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

static ProcessBuilder.Redirect INHERIT Indicates that subprocess I/O source or destination will be the same as those of the current process.

static ProcessBuilder.Redirect PIPE Indicates that subprocess I/O will be connected to the current Java process over a pipe.

So I don't think one of these will help you redirecting the output of one process to the input of another process.

So let's make our hands dirty and write some code. The relevant line is

RedirectStreams redirectStreams = new RedirectStreams(process1,process2);

where the thread, with redirection, for the processes I/O's starts. The rest of the test method is just for demonstration. For the processes I start bash and output via echo command. So for this specific example I nested echo commands.

echo Foo

Foo

echo echo

echo

public class ProcessPipeTest {
@Test public void testPipe(){
    try {

        Process process1 = new ProcessBuilder("/bin/bash").start();
        Process process2 = new ProcessBuilder("/bin/bash").start();

        RedirectStreams redirectStreams = new RedirectStreams(process1,process2);


        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(process1.getOutputStream()));
        String command = "echo echo echo";
        System.out.println("Input process1: " + command);
        bufferedWriter.write(command + '\n');
        bufferedWriter.close();


        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process2.getInputStream()));
        String actualOutput = bufferedReader.readLine();
        System.out.println("Output process2: " + actualOutput);
        assertEquals("echo",actualOutput);

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

And here the RedirectStreams class, this one can surely be designed nicer and I haven't tested, if it runs's safe, but it does the job.

public class RedirectStreams {
public RedirectStreams(Process process1, Process process2) {
    final Process tmpProcess1 = process1;
    final Process tmpProcess2 = process2;
    new Thread(new Runnable() {
        @Override
        public void run() {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(tmpProcess1.getInputStream()));
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(tmpProcess2.getOutputStream()));
            String lineToPipe;

            try {

                while ((lineToPipe = bufferedReader.readLine()) != null){
                    System.out.println("Output process1 / Input process2:" + lineToPipe);
                    bufferedWriter.write(lineToPipe + '\n');
                    bufferedWriter.flush();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}
}

Input process1: echo echo echo

Output process1 / Input process2:echo echo

Output process2: echo

 
 

Answered on 19:15, Saturday 3 Dec 2016 by sabrina ichou (17 points)
In Ingénierie logiciel pour entreprise



Do you have an answer ?