如何从Java自动启动Rserve?
我在IntelliJ IDE中编写Java应用程序。 该应用程序使用Rserve包连接到R并执行某些function。 当我想第一次运行我的代码时,我必须在命令行中启动R并启动Rserve作为守护进程,它看起来像这样:
R library(Rserve) Rserve()
执行此操作后,我可以轻松访问R中的所有函数而不会出现任何错误。 但是,由于这个Java代码将被捆绑为可执行文件,因此有一种方法可以在代码运行后自动调用Rserve(),这样我就必须跳过使用命令行启动Rserve的手动步骤?
我知道这个问题已被问到很久了。 我想你有答案。 但以下答案可能对其他人有所帮助。 这就是我发布答案的原因。 回答: – 而不是一次又一次地到R控制台启动Rserve。 你可以做的一件事是你可以编写一个java程序来启动Rserve。
下面的代码可以在java程序中使用来启动Rserve。 https://www.sitepoint.com/community/t/call-linux-command-from-java-application/3751 。 这是你将获得从java运行linux命令的代码的链接。我只更改了命令并在下面发布。
package javaapplication13; import java.io.*; public class linux_java { public static void main(String[] args) { try { String command ="R CMD Rserve"; BufferedWriter out = new BufferedWriter(new FileWriter( new File( "/home/jayshree/Desktop/testqavhourly.tab"), true)); final Process process = Runtime.getRuntime().exec(command); BufferedReader buf = new BufferedReader(new InputStreamReader( process.getInputStream())); String line; while ((line = buf.readLine()) != null) { out.write(line); out.newLine(); } buf.close(); out.close(); int returnCode = process.waitFor(); System.out.println("Return code = " + returnCode); } catch (Exception e) { e.printStackTrace(); } } }
这是我为了让Rserve
从Java
工作而编写的Class
的代码
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); } } }
- 无法解析IntelliJ中的反向路由方法
- 如何在Intellij Idea 12中重命名/移动项目?
- Intellij如何使用spring boot正确配置hql。 现在我得到持久性QL查询被错误检查
- 如何使用lib gdx导入IntelliJ中的通用补间引擎
- 在包”default package>’中找到0测试类
- JavaFX没有调用main(String args)方法
- 在IntelliJ IDEA 2017.2.1中无法使用–patch-module编译Java 9模块
- 删除JComboBox中的向下箭头
- Intellij Code Completion用于局部变量对象的所有setter / getter方法