skip to Main Content

My problem is not the connection. I have ubuntu 23.04 and I installed R (sudo apt install r-base-core) then I installed the package rserve in R (sudo -i R) and ‘install.packages("Rserve")’. Then I raise and R server (R CMD Rserve) after I download two jar necessary files: REngine.jar and RserveEngine.jar . Since here well but, my Java’s program doesn’t work well and I don’t know why.

When I compile it in the folder with the two jar’s files and my Rmatrix.java with javac -cp ./:REngine.jar:RserveEngine.jar Rmatrix.java

I am getting the following error message:

unreported exception RserveException; must be caught or declared to be thrown

repeatedly (several for REXPMismatchException, several for RserveException, and several for REngineException, all with the same sentence to which I have referred before ). I think the problem is the program structure:

import org.rosuda.REngine.REXP;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
import java.util.*;
import java.io.IOException;

public class Rmatrix {
    public static void main(String [] args) {
        RConnection con = new RConnection();
        con.eval("d = rnorm(10)");
        double[] d = con.eval("d").asDoubles();
        System.out.println(d);
        double [] x = {1,2,3,4,5};
        double[] y = {1,2,3,4,5};
        con.assign("x", x);
        con.assign("y", y);
        con.eval("z = x + y");
        double[] z = con.eval("z").asDoubles();
        System.out.println(z);
        con.close();
    }
}

I hope someone tell me how to fix it. Perhaps adding some piece of code like ‘throws IOException’ in the name of the class or ‘try{ }catch(IOException e){e.printStackTrace();}’ into the program’s code. I don’t know who to do it.

2

Answers


  1. Chosen as BEST ANSWER

    This program run perfectly:

    import org.rosuda.REngine.REXP;
    import org.rosuda.REngine.Rserve.RConnection;
    import org.rosuda.REngine.Rserve.RserveException;
    import org.rosuda.REngine.REngineException;
    import org.rosuda.REngine.REXPMismatchException;
    import java.util.*;
    import java.io.IOException;
    class Rmatrix {
    public static void main (String [] args) throws RserveException,     REXPMismatchException, REngineException 
    {
    RConnection con = new RConnection();
    REXP x = con.eval("R.version.string");
    double [] s = {1,2,2,4,7,6,7,6,9};
    double [] y = {1,2,3,4,5,6,1,2,3};
    con.assign("s", s);
    con.assign("y", y);
    con.eval("z = s + y");
    con.eval("matriz1 <- matrix( s, nrow = 3)");
    con.eval("determinante <- det(matriz1)");
    double [] z = con.eval("z").asDoubles();
    for(int i=0;i<5;i++)
     {
     double g=z[i];
     System.out.println(g);
     }
    double [] f = con.eval("determinante").asDoubles();
    double g1=f[0];
    System.out.println("El determinante de {1,2,2,4,7,6,7,6,9} es: "+g1);
    System.out.println("Adios");
    System.out.println(x.asString());
       con.close();
     }
    }
    

    And run with: 'java -cp ./:REngine.jar:RserveEngine.jar Rmatrix'

    Thank's.


  2. As the error message says, you have to handle the checked exceptions in your code. RserveException, REXPMismatchException, and REngineException are all checked exceptions, so they have to be either caught or declared thrown. The simplest solution is then (RserveException is a subclass of REngineException):

        public static void main(String[] args) throws REngineException, REXPMismatchException {
            RConnection con = new RConnection();
            con.eval("d = rnorm(10)");
           //etc
        }
    

    Of course, a more sensible approach is to catch and handle the exceptions:

    public static void main(String[] args) {
                try {
                   RConnection con = new RConnection();
                   con.eval("d = rnorm(10)");
                   //etc
                } catch(REngineException e) {
                   //your handling code          
                } catch(REXPMismatchException e) {
                   //other handling code
                }
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search