[R] Rserve() error in the first instance of running a Java application
Purva Kulkarni
purvakulkarni7 at gmail.com
Mon May 2 13:48:30 CEST 2016
Hi,
I am writing a Java GUI application which uses Rserve(). I have a java script which sends a command to the shell to start Rserve(). When I run the Java main method for the very first time, it calls this script to invoke Rserve() and starts Rserve(). But, in this first run the Java GUI application is not launched. But, when I run the main method for the second time, I get the following message and the GUI application is launched.
##> SOCK_ERROR: bind error #48(address already in use)
I do not understand this behaviour. From what I can understand, in the first run, the script just establishes the Rserve() connection and then in the second run it works. Can someone help me fix this, so that program connects to Rserve() in the first instance and also launches the GUI?
Here my Java class containing the method to invoke Rserve():
public class InvokeRserve {
public static int invoke() {
String s;
try {
// run the Unix "ps -ef" 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);
}
return 1;
}
Here is the main method which 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 {
// Start Rserve()
int value = InvokeRserve.invoke();
if(value == 1) {
// 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(“Data Analysis Application“); // Let the constructor do the job
}
});
}
}
}
More information about the R-help
mailing list