Piping External Process Output

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.

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);    }  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.