[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Piping I/O to/from child process

Posted on 1997-06-19
7
Medium Priority
?
1,850 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 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