Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Jan 7, 1998

Piping External Process Output

Question:
I am attempting to run a series of external applications within JAVA. I need the results of the prev. application piped as input to the followup application. I understand the spawning of 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 the input and output streams of a process as well as a third for reading it's standard error stream. The getOutputStream() method returns a stream through which you can write to the standard input of a process, while the getInputStream() returns a stream from which you can read the standard output of a process. getErrorStream() allows you to read the standard error output from a process.

You can use these streams to pipe output from one application to the input of another application. The processes will be multitasked by the operating system, so rather than reading all of the input from one process before passing it on to the other, you should use threads to pipe output so that a process won't stall for lack of input. The following example program 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);
    }

  }
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date