Reputation: 773
I am writing a Java
application in IntelliJ IDE. The application uses Rserve()
to connect to R
and access scripts. I have a java script which sends a command to the shell to start Rserve()
. The problem I am facing now is the first time when I run the application, I get the following error:
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)
And, when the next time I run the application, it works perfectly. From what I can understand, in the first run, it just established the Rserve()
connection and then in the second run it works. Can someone help me fix this, so that the application connects to Rserve()
in the first instance and also runs perfectly?
Here is the java class which calls Rserve()
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;
}
}
And, here is the main java class which starts the application and calls the invoke()
method:
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.");
}
}
Also, is there a way to stop an existing Rserve()
connection (I read different answers which suggest using shutdown()
, but it is not working for me)? Every time, I have to restart my computer to shutdown an existing connection and start a new connection to test the above code.
I also looked into the StartRserve.java
example file at http://www.rforge.net/DATRAS/files/org/rosuda/Mondrian/StartRserve.java. But when I use the method StartRserve.checkLocalRserve()
in the main class of my application, I get the same Rserve()
error as shown above.
Upvotes: 0
Views: 548
Reputation: 545
I have a solution for shutting Rserve connection withoutt shutting down the computer. Try this while opening an Rconnection:
RConnection rconn=null;
try{
rconn=new RConnection();
//Your personal code here
} catch(Exception exc){
exc.printStackTrace(out);
} finally{
if (rconn!=null){
rconn.close();
}
}
This way when the app stops halfway with an exception, it still runs the finally part and closes the connection. Hope this helps in time.
As for the exception-part details, it would be possible to answer if you could display the line numbers beside the code, as the exception log refers to the code by line numbers.
Upvotes: 0
Reputation: 13932
Your invoke()
method returns before Rserve is actually started, so you're apparently trying to connect too early. You may want to add p.waitFor();
. See also the StartRserve
class which is far more robust in starting Rserve from Java.
PS: I'd recommend using the stats-rosuda-devel mailing list for Rserve questions which gives you much faster responses.
Upvotes: 2