Im doing an aplication in Java and i have a program made in R what i want to launch with Java. I have the following instructions: Runtime r = Runtime.getRuntime(); try { System.out.println ("Llamada a R..."); p = r.exec(sRutaR); } catch (IOException e) { System.out.println ("Error lanzando R: " + e.getMessage()); e.printStackTrace(); } catch (Exception ex) { System.out.println ("Error lanzando R!!!! " + ex.toString()); ex.printStackTrace(); } } and after that i wait for a file that R must to make called terminado.dat. But when i launch the process the file doesn't create until i destroy the process. can anyone explain what's happend with the process? Thx in advance and sorry for my poor english
Mario de Frutos Dieguez escribi??:>Im doing an aplication in Java and i have a program made in R what i >want to launch with Java. > >I have the following instructions: > >Runtime r = Runtime.getRuntime(); > >try >{ > > System.out.println ("Llamada a R..."); > p = r.exec(sRutaR); > } > catch (IOException e) > { > System.out.println ("Error lanzando R: " + e.getMessage()); > e.printStackTrace(); > } > catch (Exception ex) > { > System.out.println ("Error lanzando R!!!! " + ex.toString()); > ex.printStackTrace(); > } >} > >and after that i wait for a file that R must to make called >terminado.dat. But when i launch the process the file doesn't create >until i destroy the process. > >can anyone explain what's happend with the process? > >Thx in advance and sorry for my poor english > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > >I think i solve the problem but i dont solve anything hehehe. But i know what's the problem. When i launch the R program like a process it enter in deadlock with the parent thread and dont finish until i stop the main thread. Anybody has suffer the same nightmare like i? and can help me? Thx in advance, and sorry for my poor english hehe
>Im doing an aplication in Java and i have a program made in R what i >want to launch with Java. > >I have the following instructions: > >Runtime r = Runtime.getRuntime(); > >try >{ > > System.out.println ("Llamada a R..."); > p = r.exec(sRutaR); > } > catch (IOException e) > { > System.out.println ("Error lanzando R: " + e.getMessage()); > e.printStackTrace(); > } > catch (Exception ex) > { > System.out.println ("Error lanzando R!!!! " + ex.toString()); > ex.printStackTrace(); > } >} > >and after that i wait for a file that R must to make called >terminado.dat. But when i launch the process the file doesn't create >until i destroy the process. > >can anyone explain what's happend with the process? > >Thx in advance and sorry for my poor english >The reason is that you did consume java output buffer, the process hanged until you clear them. Try to use following StreamGobbler.java to catch all the outputs: //start of StreamGobbler.java import java.util.*; import java.io.*; class StreamGobbler extends Thread { InputStream is; String type; StreamGobbler(InputStream is, String type) { this.is = is; this.type = type; } public void run() { try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line=null; while ( (line = br.readLine()) != null) System.out.println(type + ">" + line); } catch (IOException ioe) { ioe.printStackTrace(); } } } //end of StreamGobbler then use this method to do system call: public boolean execWait(String comm){ try{ Process proc = Runtime.getRuntime().exec(comm); StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR"); // any output? StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT"); // kick them off errorGobbler.start(); outputGobbler.start(); int returnVal = proc.waitFor(); if (returnVal != 0) { return(false); } } catch (Exception e){ e.printStackTrace(); return false; } return true; } Hope this helps, Junwen