Solved

Piping I/O to/from child process

Posted on 1997-06-19
7
1,811 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
Comment Utility
Have you tried using the Sistem.In stream?
0
 

Author Comment

by:seanbreslin
Comment Utility
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
Comment Utility
Actualy Ssyem.in reads data from the standard input, which is the hardware, but can be redirected.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Accepted Solution

by:
Philippe earned 100 total points
Comment Utility

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
Comment Utility
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
Comment Utility

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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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.

771 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

10 Experts available now in Live!

Get 1:1 Help Now