Solved

Piping I/O to/from child process

Posted on 1997-06-19
7
1,827 Views
Last Modified: 2012-08-13
I would like to setup input and output pipes to a child
process spawned with exec(). I have managed to connect a
pipe to the output of the child but have not been able to
setup the input pipe.

import java.io.*;

public class Child{
      String str, child="child.exe";
      String command="command 1";

      try{
         Process cildProcess =                    Runtime.getRuntime().exec(child);

         // Pipe stdout from child to my class
         DataInputStream inPipe = new DataInputStream(
                  new BufferedInputStream(
                    childProcess.getInputStream()));
            
/* if this section is commented out the rest of the code works
         // Pipe stdin to Child from my class
         DataOutputStream outPipe = new DataOutputStream(
                        new BufferedOutputStream(
                        Process.getOutputStream()));

         // output command to child process
         outPipe.writeChars(command);
 commented out */

         // while loop reads input from child
         while(true){
            str = inPipe.readLine();
            if(str == null) exit(0);
            System.out.println(str);
            }
         }
      catch (IOException e) {
          System.out.println("Error: " + e);
          System.exit(-1);
          }
}

with the section commented out this code works but when
I leave in this section java locks up, running under
windows 95'

Any help would be very appriciated
Sean Breslin
0
Comment
Question by:seanbreslin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1221806
Have you tried using the Sistem.In stream?
0
 

Author Comment

by:seanbreslin
ID: 1221807
Well the System.in stream connects directly to my computers h/w.
I want to receive i/o from a program that I have launched from my java program.
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1221808
Actualy Ssyem.in reads data from the standard input, which is the hardware, but can be redirected.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 2

Accepted Solution

by:
Philippe earned 100 total points
ID: 1221809

seanbreslin,

You forgot to flush your output stream. Indeed, it is a buffered stream, so it will only write to the child process once its buffer is full or when you call outPipe.flush.

Also, make sure your child process will reply something after receiving a string. Unless they read their input character by character, most programs will only read their input if it is terminated by a carriage return. This is why I added a \n in the command string. Your child.exe may or may not need that.

  cheers,

    Philippe

Here is my corrected code (runs for unix, should run for you unless its there is an additional win95 problem)

import java.io.*;

public class pipes{

  String str, child="/usr/bin/cat";
  String command="command 1\n";

  public pipes (){
    try{

      Process childProcess =  Runtime.getRuntime().exec(child);

      // Pipe stdout from child to my class
      DataInputStream inPipe = new
      DataInputStream(new BufferedInputStream(childProcess.getInputStream()));
     
      /* if this section is commented out the rest of the code works */
      // Pipe stdin to Child from my class
      DataOutputStream outPipe = new
    DataOutputStream(new BufferedOutputStream(childProcess.getOutputStream()));

      // output command to child process
      outPipe.writeChars(command);
      outPipe.flush();
     
      // while loop reads input from child
      while(true){
      str = inPipe.readLine();
      if(str == null) System.exit(0);
      System.out.println(str);
      }
    }

    catch (IOException e) {
      System.out.println("Hoppela,  " + e);
      System.exit(-1);
    }

  }

  public static void main(String argv[]) {
    pipes a = new pipes();
  }
}
 

0
 

Author Comment

by:seanbreslin
ID: 1221810
Philippe, I tried your solution on my machine and it still locks
up! A friend of mine is going to compile it and run it on his
sparc station and let me know if it works there. I guess the problem is with windows 95 if he can get it to run on his sparc.
I also tried it under NT with no luck!
0
 
LVL 2

Expert Comment

by:Philippe
ID: 1221811

Seanbreslin,

Just forgot to sayt that you should make sure your child.exe is not buffering its output.

An easy way to debug this is to have child.exe doing output to stdout and a logfile at the same time. So you see where it gets stuck.

  good luck

   Philippe

0
 

Author Comment

by:seanbreslin
ID: 1221812
I wrote a small, 5 lines of code, C program that uses gets() to read from the keyboard and printf() to print to screen, no buffering!

Sean
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question