【问题标题】:How to start Rserve automatically from Java?如何从 Java 自动启动 Rserve?
【发布时间】:2015-09-03 10:40:55
【问题描述】:

我正在 IntelliJ IDE 中编写 Java 应用程序。该应用程序使用 Rserve 包连接到 R 并执行一些功能。当我想第一次运行我的代码时,我必须在命令行中启动 R 并将 Rserve 作为一个守护进程启动,它看起来像这样:

R
library(Rserve)
Rserve()

完成此操作后,我可以轻松访问 R 中的所有函数而不会出现任何错误。但是,由于此 Java 代码将被捆绑为可执行文件,因此有没有一种方法可以在代码运行后立即自动调用 Rserve(),这样我就必须跳过使用命令行启动 Rserve 的手动步骤?

【问题讨论】:

    标签: java r intellij-idea rserve


    【解决方案1】:

    这是我写的 Class 的代码,用于让 RserveJava 工作

    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);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      我知道很久以前就有人问过这个问题。我想你有答案。但以下答案可能对其他人有所帮助。这就是为什么我要发布我的答案。 答案:- 而不是一次又一次地去 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();
              }
                    }
                 }
      

      【讨论】:

      • 我试过这段代码,这最初对我不起作用。然后我发现我已经使用 Rstudio Rserve 进行了安装,因此控制台上的“R CMD Rserve”失败了。然后我从 Rserve 网站下载了二进制文件并使用命令行安装了它。如果有人遇到同样的问题,希望它会有所帮助。