Solved

Piping I/O to/from child process

Posted on 1997-06-19
7
1,814 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

937 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

Need Help in Real-Time?

Connect with top rated Experts

3 Experts available now in Live!

Get 1:1 Help Now