s.o.s problems comunicating external processes in java under linux
I've programmed a little program in C called "cinterpreter" which works like an interpreter, when it launches it shows a welcome message to the display (standart output), then is always waiting for strings from the keyboard showing the length of the input strings until the "quit" string is received, in that case the C program named "cinterpreter" finish, here we can see the code:
-
# include <stdio.h>
# include <string.h>
char presentacion[8][80] = {
" ||||||||||||||||||",
" Welcome to Maude ",
" ||||||||||||||||||",
" Maude version 1.0.5 built: Apr 5 2000 15:56:52",
" Copyright 1997-2000 SRI International",
" Thu Aug 9 13:40:25 2001",
" ",
" "};
void longitud(char * cadena)
{
int t;
for(t=0;t<5;t++)
printf("MAUDE_OUT> la cadena %s tiene %d caracteres\n",cadena,strlen(cadena));
}
int main()
{
int i;
char cadena[80];
for(i=0;i<8;++i) printf("%s\n",presentacion);printf("\n");
while( strcmp(cadena,"quit") !=0)
{
printf("MAUDEENTRADA>");
scanf("%s",cadena);
longitud(cadena);
}
}
I'm intereted in running this program, control it's standart input and
starndart output through a java program through the Runtime , process class and the correponding methods like "exec", getInputStream, getOutputStream, getErrorStream, the question is that I do not Know what I'm doing wrongly but I don't get what I want, first of all, I can't get the welcome presentation, and if I give to the external process "cinterprete" a string different than the "quit" string I can not get anything from de external process, here it is the java code I'm using, I would apreciate any help from anyone interested in resolving S.O.S problems, here I give my e-mail adress:
e-mail:
charliesildavia@hotmail.com
charliesildavia@terra.es
*************** java code **********************
import java.io.*;
class Streamhilo extends Thread
{
InputStream in;
String type;
Streamhilo(InputStream in, String type)
{
super("hilo " + type);
this.in = in;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
class Streamout extends Thread
{
OutputStream ouut;
String cadena;
Streamout(OutputStream ouut, String cadena)
{
super("hilo " + "ESCRITURA");
this.ouut = ouut;
this.cadena = cadena;
}
public void run()
{
try
{
OutputStreamWriter isr = new OutputStreamWriter(ouut);
BufferedWriter br = new BufferedWriter(isr);
br.write(cadena);
br.flush();
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public class mio {
public static void main(String[] args)
{
String programa = "/usr/bin/cinterprete";
File fichero = new File(programa);
if (fichero.exists())
{
try {
Runtime r = Runtime.getRuntime();
Process proc = r.exec(programa);
Streamout inhilo = new Streamout(proc.getOutputStream(),"olo \n");
Streamhilo outputhilo = new Streamhilo(proc.getInputStream(),"SALIDA");
inhilo.start();
inhilo.join();
outputhilo.start();
outputhilo.join();
}catch (Exception t) {System.out.println("error de ejecucion");
t.printStackTrace();}
}
else System.out.println("maude no encontrado \n");
System.out.println("PROGRAM ENDS");
}
}

