tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > IO > External Process Output

External Process Output 

The following example shows how to capture the output from an external process. This process could be a DOS Batch script process, VB script process or any other process. Please note that the Batch process's input stream becomes output and output stream becomes input for your java program.

File Name  :  
com/bethecoder/tutorials/io/ExternalProcOutput.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.io;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExternalProcOutput {

  public static void main(String[] args) {

    String line;
    try {
      Process proc = Runtime.getRuntime().exec("C:\\ex_proc_out.bat");
      BufferedReader procInput = new BufferedReader(
          new InputStreamReader(proc.getInputStream()));
      
      //Read output from batch process
      while ((line = procInput.readLine()) != null) {
        System.out.println(line);
      }
      
      procInput.close();
      
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
   

The DOS Batch file referred is shown below,
File Name  :  
source/com/bethecoder/tutorials/io/ex_proc_out.bat 
@echo off
echo Welcome
echo Hello World
echo Good ni8

It gives the following output,
Welcome
Hello World
Good ni8



 
  


  
bl  br