By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,478 Members | 1,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

runtime.exec child process - C program, I/O blocked

P: 6
Hi everyone , i am banging my head over the following problem for a couple of weeks now: i am starting a MyProg.exe(a simple C app) from a java class with .exec() as a system process(not in it's own console) and i would like to communikcate with its I/O before it is finished. I don't have this issue if for instance i am starting another java app.

The problem i meet is that the I/O is bloked while the process is being executed.When i terminate it , it is all released but it doesn't do me any good...

Here is some source , i'd really a ppreciate some help as i am stuck here....

Expand|Select|Wrap|Line Numbers
  1.  String[] command={"cmd","/c","prog.exe"};
  2.             Runtime rt = Runtime.getRuntime();
  3.             Process proc = rt.exec(command);
  4.             InputStream stderr = proc.getInputStream();
  5.             PrintWriter out = new PrintWriter(new OutputStreamWriter(proc.getOutputStream()), true);
  6.  
  7.             String str="123";
  8.             BufferedOutputStream bufStr= (BufferedOutputStream) proc.getOutputStream();
  9.             bufStr.write(231);
  10.             bufStr.flush();
  11.  
  12.             InputStreamReader isr = new InputStreamReader(stderr);
  13.             BufferedReader br = new BufferedReader(isr);
  14.             String line = null;
  15.             while ( (line = br.readLine()) != null)
  16.                 System.out.println(line);
  17.  
  18.  
  19.             int exitVal = proc.waitFor();
  20.             System.out.println("Process exitValue: " + exitVal);
  21.         } catch (Throwable t)
  22.           {
  23.             t.printStackTrace();
  24.           }
  25.  
the C prog i am running:

Expand|Select|Wrap|Line Numbers
  1.  #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. void main(void){
  5.  
  6.     int i;
  7.     printf("sdfsdcds");
  8.     scanf("%d",&i);
  9.     printf(" \n %d",i);
  10.  
  11. }
Jun 12 '07 #1
Share this Question
Share on Google+
8 Replies


Expert 10K+
P: 11,448
Hi everyone , i am banging my head over the following problem for a couple of weeks now: i am starting a MyProg.exe(a simple C app) from a java class with .exec() as a system process(not in it's own console) and i would like to communikcate with its I/O before it is finished. I don't have this issue if for instance i am starting another java app.

The problem i meet is that the I/O is bloked while the process is being executed.When i terminate it , it is all released but it doesn't do me any good...

Here is some source , i'd really a ppreciate some help as i am stuck here....

Expand|Select|Wrap|Line Numbers
  1. String[] command={"cmd","/c","prog.exe"};
  2.             Runtime rt = Runtime.getRuntime();
  3.  
When you're starting an executable image there's no need to start a shell (cmd)
first; just use "prog.exe" as the command.

Expand|Select|Wrap|Line Numbers
  1.             String str="123";
  2.             BufferedOutputStream bufStr= (BufferedOutputStream) proc.getOutputStream();
  3.             bufStr.write(231);
  4.             bufStr.flush();
  5.  
Note that you're just writing a single byte here; not what your C process expects.
Your C process expects a number, so better write "123\n" to the process.

kind regards,

Jos
Jun 12 '07 #2

P: 6
Thanks for the input Jos but it doesn't help me. My problem is that the (C prog) exe process seems to be blocking his ouput stream until finishing!!? Which i can't see happening when i do the same with a Java app.

What i also find interesting is that my Java basic class which starts the child (exe) thread seems to hang waiting exactly at the line where i ask it to read from the InputStream....

if it rings a bell please write, anyone :)
Jun 13 '07 #3

Expert 10K+
P: 11,448
Thanks for the input Jos but it doesn't help me. My problem is that the (C prog) exe process seems to be blocking his ouput stream until finishing!!? Which i can't see happening when i do the same with a Java app.

What i also find interesting is that my Java basic class which starts the child (exe) thread seems to hang waiting exactly at the line where i ask it to read from the InputStream....

if it rings a bell please write, anyone :)
If your thread seems to 'hang' the C process isn't procucing any output and your
Java process is waiting for it. In the mean time your Java process can't send any
input to that other C process either. I always use different threads for reading and
writing from/to external processes.

kind regards,

Jos
Jun 13 '07 #4

P: 6
The thing is that my 'C' prog produces output - a couple of printf statements , and the innput towards the "C"prog is not blocked. For instance i have a couple of printfs and scanfs - if i push some data to fill the scanfs , after the "C" prog is termianted the whole ouput comes in the console....

I am trying to emulate a interaction with this "C"prog through a java written web service for a browser, and that is why this doesn't work for me. I have also tryed to separate all the streams in different threads with the same result...
Jun 14 '07 #5

P: 2
Hello, I have the same problem with interaction from Java app to C app. My C app gives some output and waits for input. I read its stdout and stderr streams in separate threads, and in java main thread I push some data to C app input.

My stderr/stdout stream consumers also hang, like C app doesn't print anything to console. Also, if it prints something and then exits, I receive all its output in my consumers.

It seems like C app's output just hangs out in some buffer until it's finished. Please post here if you find a solution...
Jun 19 '07 #6

Expert 10K+
P: 11,448
Hello, I have the same problem with interaction from Java app to C app. My C app gives some output and waits for input. I read its stdout and stderr streams in separate threads, and in java main thread I push some data to C app input.

My stderr/stdout stream consumers also hang, like C app doesn't print anything to console. Also, if it prints something and then exits, I receive all its output in my consumers.

It seems like C app's output just hangs out in some buffer until it's finished. Please post here if you find a solution...
Don't use buffered input streams then for reading from stdout and/or stderr.
If your C process doesn't properly flushes its output streams the streams will
only be flushed when the process exits. I'm almost sure it's a buffer problem;
same for you and the OP who's thread you hijacked ;-)

kind regards,

Jos
Jun 19 '07 #7

P: 2
Don't use buffered input streams then for reading from stdout and/or stderr.
If your C process doesn't properly flushes its output streams the streams will
only be flushed when the process exits. I'm almost sure it's a buffer problem;
same for you and the OP who's thread you hijacked ;-)

kind regards,

Jos
I don't use buffered input streams, just an InputStreamReader around Process's InputStream (which is its stdout). Anyway, process.getInputStream().available() always returns 0...

I just tried to call fflush(stdout) in my C app after printf(), and it works! I got its stdout in my java program exactly where I want :)
thanks for advice!

But I do see my app's output when I run it in console, without any explicit fflush-es. Why it is flushed "automatically" when I run it manually, and does not when I run it from JVM.... Modifying C app is a good workaround, but it won't help if I cannot modify my C app's code (and that is the case actually).
Jun 19 '07 #8

Expert 10K+
P: 11,448
I don't use buffered input streams, just an InputStreamReader around Process's InputStream (which is its stdout). Anyway, process.getInputStream().available() always returns 0...

I just tried to call fflush(stdout) in my C app after printf(), and it works! I got its stdout in my java program exactly where I want :)
thanks for advice!

But I do see my app's output when I run it in console, without any explicit fflush-es. Why it is flushed "automatically" when I run it manually, and does not when I run it from JVM.... Modifying C app is a good workaround, but it won't help if I cannot modify my C app's code (and that is the case actually).
The C process console driver behaves differently from the one where no console
is present; the latter is buffered and won't show up until either the buffer is full
or the process terminates. Normal console output streams are just line buffered.

It's not Java's fault, it's the other side that acts up ;-)

kind regards,

Jos
Jun 19 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.