Question

I am trying to pass a double array into R, sum its values, and return it to Java. Here is what I am trying to do in Java:

import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;

// Start R session.
Rengine re = new Rengine (new String [] {"--vanilla"}, false, null);

// Check if the session is working.
if (!re.waitForR()) {
    return;
}

re.assign("x", new double[] {1.5, 2.5, 3.5});
REXP result = re.eval("(sum(x))");
System.out.println(result.asDouble());
re.end();

However, I get the errors: import org.rosuda.JRI.REXP cannot be resolved import org.rosuda.JRI.Rengine cannot be resolved Rengine cannot be resolved to a type

This is the case even if for the imports I do:

import java.lang.Object.org.rosuda.JRI.REXP;
import java.lang.Object.org.rosuda.JRI.Rengine;

Any advice? Thank you!!

Was it helpful?

Solution

Your imports should be:

import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;

Appending java.lang.Object in front of them creates rubbish. To resolve the compile errors you are getting make sure to include the (correct version of the) JRI-x-.x-x.jar in your build classpath. Eg.

javac -cp ".:/some/path/to/JRI-0.8-4.jar" *.java

If you are using an IDE then include the JAR in the projects build path. So for example, using Eclipse you would add the JAR to a (regular project) by right-clicking the project, then navigating to Properties->Java Build Path->Add External JARs, locating and selecting the JAR, then clicking OK out of all dialog boxes. Your imports should then resolve cleanly:

Eclipse Project with JRI Library on Build Path

Note that JRI-0.8-4.jar shows up under Referenced Libraries, for the project

OTHER TIPS

As I said in your previous question, I would highly recommend you (or anyone visiting this thread in the future) to use this plugin to set up JRI in Eclipse. It does everything for you. The page I linked steps you through the process.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top