[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Piping I/O to/from child process

Posted on 1997-06-19
7
Medium Priority
?
1,841 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
Philippe earned 300 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

650 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