473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
8 9559
JosAH
11,448 Expert 8TB
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
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
JosAH
11,448 Expert 8TB
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
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
smok
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
JosAH
11,448 Expert 8TB
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
smok
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
JosAH
11,448 Expert 8TB
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

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

Similar topics

4
by: Hal Vaughan | last post by:
I never noticed this before, but when I run an external program through Runtime, like this: Runtime rt = Runtime.getRuntime(); System.out.println("Running external command: " + sCommand); try...
1
by: Hal Vaughan | last post by:
I've been using Runtime.exec() like this: Runtime rt = Runtime.getRuntime(); try {Process p = rt.exec("MyCommand.bat");} catch (Exception e) {do stuff} When I start my Java classes, I start...
5
by: JW | last post by:
Hi, I don't seem to get any results from the following use of Runtime.getRuntime().exec(cmd) using Java 1.4 on Redhat linux. Suppose that in the same directory as my java file below, I have...
2
by: uwnewsgroup | last post by:
When I was using Runtime.exec(String cmd) to run a unix utility (join), and try to get its standard output by using Process.getOutputStream(), it blocks forever. I tried it using Java 1.4 and...
2
by: gnutuxy | last post by:
Hi, I am newbie in the Unix system programming and in learning phase. I usually read the libc manual and then try to implement small programs to test/check the learnt thing. I read the libc...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
5
by: chenthil | last post by:
In my Java program I need to call two DOS batch programs namely call.bat and start.bat. First I need to start the batch program call.bat and once that program is completed, I need to call the other...
2
by: jerry chapman | last post by:
I am trying to send a command to windows from my java program, and I get an error. The pertinent (?) part of my code follows: public boolean action(Event evt, Object arg) { if...
3
by: Shayco | last post by:
hey, in my code i'm using Runtime.getRuntime().exec() in order to run a .bat file that calls another java program (they communicate with each other using RMI). when i call: Process process =...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.