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");

}

}

[4059 byte] By [charliesildavia] at [2007-9-26 4:18:29]
# 1

I have hacked your code a bit. It might be of some help. BTW love the error message "error de ejacucion" Also note that I have not handled the 'c' programs termination properly

C 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));

}

fflush(stdout);

}

int

main ()

{

int j;

char cadena[80];

for (j = 0; j < 8; j++)

printf ("%s\n", presentacion[j]);

printf ("\n");

fflush(stdout);

while (strcmp (cadena, "quit") != 0)

{

printf ("MAUDEENTRADA>");

fflush(stdout);

scanf ("%s", cadena);

longitud (cadena);

}

}

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 isw = new OutputStreamWriter (ouut);

BufferedWriter bw = new BufferedWriter (isw);

InputStreamReader isr = new InputStreamReader (System.in);

BufferedReader br = new BufferedReader (isr);

String line = null;

while ((line = br.readLine ()) != null) {

bw.write (line + "\n");

bw.flush ();

}

} catch (IOException ioe) {

ioe.printStackTrace ();

}

}

}

public class mio {

public static void main (String[]args) {

String programa = "a.out";

File fichero = new File (programa);

if (fichero.exists ()) {

try {

Runtime r = Runtime.getRuntime ();

Process proc = r.exec (programa);

Streamout inhilo =

new Streamout (proc.getOutputStream (), "Hello\n");

Streamhilo outputhilo =

new Streamhilo (proc.getInputStream (), "SALIDA");

inhilo.start ();

outputhilo.start ();

inhilo.join ();

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");

}

}

rob_canoe2 at 2007-6-29 17:20:25 > top of Java-index,Archived Forums,Java Programming...