interacting with a running c program
hi all..
i have a c program that i want to interact with from java, using processes the code for the
c program is like so...
#include <stdio.h>
int main()
{
char sentence[ 80 ];
char c;
int i = 0;
printf("Enter a line of text:\n" );
while( ( c = getchar() )!='\n' ){
sentence[ i++ ] = c;
}
sentence[ i ] ='\0';
puts("\nthe line you entered:" );
puts( sentence );
return 0;
}
and the code for the java program is like so
import java.util.*;
import java.io.*;
class StreamGobblerextends Thread
{
InputStream is;
String type;
OutputStream os;
StreamGobbler( InputStream is ,OutputStream os, String type)
{
this.is = is;
this.type = type;
this.os = os;
}
publicvoid run()
{
try
{
OutputStreamWriter osw =new OutputStreamWriter(os);
//BufferedWriter bw = new BufferedWriter(osw);
InputStreamReader isr =new InputStreamReader(is);
BufferedReader br =new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) !=null)
System.out.println(line);
//bw.write("tom is me");
//bw.write( "\n" );
osw.write("just testing");
osw.write("\n");
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
publicclass GoodWinExec
{
publicstaticvoid main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("/home/generic-coder/programming/c/enterchar");
// any error message?
StreamGobbler errorGobbler =new
StreamGobbler(proc.getErrorStream(), proc.getOutputStream() ,"ERROR");
// any output?
StreamGobbler inputGobbler =new
StreamGobbler(proc.getInputStream(),proc.getOutputStream(),"IN");
// any output?
StreamGobbler outputGobbler =new
StreamGobbler(proc.getInputStream(), proc.getOutputStream(),"OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
inputGobbler.start();
// any error?
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
}catch (Throwable t)
{
t.printStackTrace();
}
}
}
when i run the program i dont get anything from the c program i want to run.
does anybody know what i am doing wrong?
i am new to programming using processes ....
your help will be highly acceptable ...

