I am running an R script from java using RScript and Runtime.getRuntime() as shown in the code below. The code seems to stop part way through (the code stops writing output). If I run the script from a cmd line the script runs to completion. I'm not sure if this is a problem with RScript, or maybe the buffered reader, or maybe Runtime.getRuntime().exec().
Any help would be greatly appreciated.
package org.nachc.tools.fhirtoomop.tools.build.postgres.build;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import com.nach.core.util.file.FileUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ETLSYN01_LoadSynthFiles {
public static void main(String[] args) {
exec();
}
public static void exec() {
try {
log.info("\n\n\n-----------------------------------");
log.info("LOADING TEST DATA USING ETL-Synthea R-SCRIPTS");
// get the file to run
File file = FileUtil.getFile("/postgres/build/r/load-synthea-files.R");
String path = FileUtil.getCanonicalPath(file);
String cmd = "Rscript " + path;
log.info("Got file: " + path);
log.info("Running cmd: " + cmd);
// run r through runtime
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
// echo output
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
log.info(s);
}
// echo errors
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
log.info(s);
}
log.info("Done.");
} catch(Exception exp) {
throw (new RuntimeException(exp));
}
}
}
--- EDIT ----------------------------------
If I add sink to my code, the Java process keeps running and the output to the file stops at the exact same point where it stops when output is sent to the console normally
sink(file="./SINK.TXT")
...<my_code/>
sink(file=NULL)
--- EDIT ----------------------------------
As a potential workaround, calling rscript from a .bat file works (the rscript runs to completion). The code below runs to completion.
package org.nachc.tools.fhirtoomop.tools.build.postgres.build;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.yaorma.util.time.Timer;
import com.nach.core.util.file.FileUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ACH1_RunAchilles {
public static void main(String[] args) {
exec();
}
public static void exec() {
Timer timer = new Timer();
timer.start();
// runRScript(FileUtil.getFile("/postgres/build/r/achilles/install-achilles.r"));
File file = FileUtil.getFile("/postgres/build/r/achilles/run-achilles.bat");
String fileName = FileUtil.getCanonicalPath(file);
log.info("File: " + fileName);
try {
Runtime.
getRuntime().
exec("cmd /c start \"\" " + fileName);
} catch (Exception exp) {
throw new RuntimeException(exp);
}
log.info("TIME TO RUN ACHILLES: " + timer.getElapsedString());
log.info("Done running Ahcilles.");
}
private static void runRScript(File file) {
try {
log.info("\n\n\n-----------------------------------");
log.info("RUNNING ACHILLES USING R-SCRIPTS");
// get the file to run
String path = FileUtil.getCanonicalPath(file);
String cmd = "Rscript " + path;
log.info("Got file: " + path);
log.info("Running cmd: " + cmd);
// run r through runtime
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
// echo output
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String s;
s = null;
while ((s = stdInput.readLine()) != null) {
log.info(s);
}
log.info("Done with Achilles, looking to see if there are any errors...");
// echo errors
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
System.out.println("Here is the standard error of the command (if any):\n");
s = null;
boolean hasErrors = false;
while ((s = stdError.readLine()) != null) {
if (hasErrors == false) {
hasErrors = true;
log.error("ERRORS LOGGED DURING EXECUTION");
}
log.info(s);
}
log.info("Done processing R script");
} catch (Exception exp) {
throw (new RuntimeException(exp));
}
}
}