devxlogo

Piping External Process Output

Piping External Process Output

Question:
I am attempting to run a series of externalapplications within JAVA. I need the resultsof the prev. application piped as input to thefollowup application. I understand the spawningof the external process, but am having trouble understanding how to pipe the output.

Answer:
The java.lang.Process class provides two methods to get a handle on theinput and output streams of a process as well as a third for readingit’s standard error stream. The getOutputStream() method returns astream through which you can write to the standard input of a process,while the getInputStream() returns a stream from which you can read thestandard output of a process. getErrorStream() allows you to read thestandard error output from a process.

You can use these streams to pipe output from one application to the inputof another application. The processes will be multitasked by the operatingsystem, so rather than reading all of the input from one process beforepassing it on to the other, you should use threads to pipe output sothat a process won’t stall for lack of input. The following exampleprogram demonstrates how to do this, although it lacks real error handling.The program pipes the output of the BSD Unix command “ps -aux” to “grep nfs,”listing all running programs containing nfs in their names to System.out.

import java.io.*;public final class PipedOutput {  public static final void copyStream(InputStream input, OutputStream output)       throws IOException  {    int bytesRead;    byte[] buffer = new byte[1024];    while((bytesRead = input.read(buffer)) != -1) {      output.write(buffer);      output.flush();    }  }  public static final void main(String[] args) {    Runtime runtime;    Process psProc;    final Process grepProc;    Thread outputThread;    InputStream psInput;    OutputStream grepOutput;    String psCommand = "/bin/ps -aux", grepCommand = "/bin/grep nfs";    runtime = Runtime.getRuntime();    try {      psProc = runtime.exec(psCommand);      grepProc = runtime.exec(grepCommand);    } catch(IOException e) {      e.printStackTrace();      System.exit(1);      return;    }    // We need a separate thread to capture the grep output as it is    // produced and write it to standard output.  The processes are    // multitasked by the OS, so we don't want to serialize I/O    // processing.  If you want to chain output from more processes, you    // would need a new thread for each process.  You could create a    // generic class with real error handling to do this.    outputThread = new Thread() {      public void run() {	try {	  PipedOutput.copyStream(grepProc.getInputStream(), System.out);	} catch(IOException e) {	  e.printStackTrace();	  System.exit(1);	}      }    };    psInput    = psProc.getInputStream();    grepOutput = grepProc.getOutputStream();    outputThread.start();    try {      PipedOutput.copyStream(psInput, grepOutput);      // Closing the output stream will wind up terminating the grep process      // because grep will see EOF.      grepOutput.close();    } catch(IOException e) {      e.printStackTrace();      System.exit(1);    }  }}
See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist