【问题标题】:Rserve() error in the first instance of running a Java application运行 Java 应用程序的第一个实例中的 Rserve() 错误
【发布时间】:2016-04-20 12:38:58
【问题描述】:

我正在 IntelliJ IDE 中编写 Java 应用程序。该应用程序使用Rserve() 连接到R 并访问脚本。我有一个 java 脚本,它向 shell 发送命令以启动Rserve()。我现在面临的问题是第一次运行应用程序时,出现以下错误:

Exception in thread "main" java.lang.NoSuchMethodError: org.rosuda.REngine.REngineException.<init>(Lorg/rosuda/REngine/REngine;Ljava/lang/String;Ljava/lang/Throwable;)V
    at org.rosuda.REngine.Rserve.RserveException.<init>(RserveException.java:59)
    at org.rosuda.REngine.Rserve.RConnection.<init>(RConnection.java:90)
    at org.rosuda.REngine.Rserve.RConnection.<init>(RConnection.java:60)
    at org.rosuda.REngine.Rserve.RConnection.<init>(RConnection.java:44)
    at de.uni_jena.bioinformatik.RecalibrationGUI.Application.main(Application.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

而且,当我下次运行该应用程序时,它可以完美运行。据我了解,在第一次运行中,它只是建立了Rserve() 连接,然后在第二次运行中它可以工作。有人可以帮我解决这个问题,以便应用程序首先连接到Rserve() 并完美运行吗?

这里是调用Rserve()的java类

public class InvokeRserve {
    public static boolean invoke() {
        try {
            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()));
        }
        catch (IOException e) {
            System.out.println("Rserve connection: Exception occurred: ");
            e.printStackTrace();
            System.exit(-1);
        }
        return true;
    }
}

而且,这里是启动应用程序并调用invoke() 方法的主要java 类:

public class Application {
    /**
     * Main method for the Swing application
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
            boolean value = InvokeRserve.invoke();
        RConnection check = new RConnection();
        if(check.isConnected())
        {
            // Run the GUI construction in the Event-Dispatching thread for thread-safety
            SwingUtilities.invokeLater(new Runnable() {

                /**
                 * run method which create a new GUIMain object
                 */
                @Override
                public void run() {new GUIMain("Java Application"); // Let the constructor do the job
                }
            });
       }
        else
        System.out.println("There was no R connection found.");
    }
}

另外,有没有办法停止现有的Rserve() 连接(我阅读了建议使用shutdown() 的不同答案,但它对我不起作用)?每次,我都必须重新启动计算机以关闭现有连接并启动新连接以测试上述代码。

我还查看了http://www.rforge.net/DATRAS/files/org/rosuda/Mondrian/StartRserve.javaStartRserve.java 示例文件。但是当我在我的应用程序的主类中使用StartRserve.checkLocalRserve() 方法时,我得到了与上图相同的Rserve() 错误。

【问题讨论】:

    标签: java r rserve


    【解决方案1】:

    您的 invoke() 方法在 Rserve 实际启动之前返回,因此您显然试图过早连接。您可能需要添加p.waitFor();。另请参阅 StartRserve 类,它在从 Java 启动 Rserve 时更加健壮。

    PS:对于 Rserve 问题,我建议您使用 stats-rosuda-devel mailing list,这样可以更快地回复。

    【讨论】:

      【解决方案2】:

      我有一个解决方案,可以在不关闭计算机的情况下关闭 Rserve 连接。在打开 Rconnection 时试试这个:

          RConnection rconn=null;
          try{
              rconn=new RConnection();
              //Your personal code here
      
          } catch(Exception exc){
              exc.printStackTrace(out);
          } finally{
              if (rconn!=null){
                  rconn.close();
              }
          }
      

      这样当应用程序中途停止异常时,它仍然会运行 finally 部分并关闭连接。 希望这能及时提供帮助。

      关于异常部分的详细信息,如果您可以在代码旁边显示行号,则可以回答,因为异常日志是按行号引用代码的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-28
        • 1970-01-01
        相关资源
        最近更新 更多