【发布时间】: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.java 的StartRserve.java 示例文件。但是当我在我的应用程序的主类中使用StartRserve.checkLocalRserve() 方法时,我得到了与上图相同的Rserve() 错误。
【问题讨论】: