如何从Java自动启动Rserve?
发布时间:2020-12-15 04:29:26 所属栏目:Java 来源:网络整理
导读:我在IntelliJ IDE中编写 Java应用程序.该应用程序使用Rserve包连接到R并执行某些功能.当我想第一次运行我的代码时,我必须在命令行中启动R并启动Rserve作为守护进程,它看起来像这样: Rlibrary(Rserve)Rserve() 执行此操作后,我可以轻松访问R中的所有函数而不
|
我在IntelliJ IDE中编写
Java应用程序.该应用程序使用Rserve包连接到R并执行某些功能.当我想第一次运行我的代码时,我必须在命令行中启动R并启动Rserve作为守护进程,它看起来像这样:
R library(Rserve) Rserve() 执行此操作后,我可以轻松访问R中的所有函数而不会出现任何错误.但是,由于这个Java代码将捆绑为可执行文件,因此有一种方法可以在代码运行后自动调用Rserve(),这样我就必须跳过使用命令行启动Rserve的手动步骤? 解决方法
这是我为了让Rserve从Java工作而编写的类的代码
public class InvokeRserve {
public static void invoke() {
String s;
try {
// run the Unix ""R CMD RServe --vanilla"" command
// using the Runtime exec method:
Process p = Runtime.getRuntime().exec("R CMD RServe --vanilla");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
// System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
