我正在使用 RScript 和 Runtime.getRuntime() 从 java 运行 R 脚本,如下面的代码所示。代码似乎中途停止(代码停止写入输出)。如果我从 cmd 行运行脚本,脚本将运行到完成。我不确定这是 RScript 的问题,还是缓冲阅读器的问题,或者 Runtime.getRuntime().exec().
任何帮助将不胜感激。
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));
}
}
}
---编辑--------------------------------
如果我将 sink 添加到我的代码中,Java 进程将继续运行,并且到文件的输出将在输出正常发送到控制台时停止的完全相同的位置停止
sink(file="./SINK.TXT")
...<my_code/>
sink(file=NULL)