4

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!!

Michael Buen
  • 38,643
  • 9
  • 94
  • 118

2 Answers2

0

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

Perception
  • 79,279
  • 19
  • 185
  • 195
  • I really do not have much experience with this. I am not even sure what you mean by "classpath". I am using a Mac, by the way. I have not used JRI before, it was recommended to me, and all I have done is typed those two lines at the top of my program. Sorry, I am not understanding... :o( –  Apr 15 '13 at 22:11
  • You need to start at the beginning then. With a [basic Java tutorial](http://docs.oracle.com/javase/tutorial/getStarted/). Then refer to [here](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html) for information on setting classpaths for running your program. – Perception Apr 15 '13 at 22:18
  • For classpath, do you just mean the path to the .src file of the JAVA code for which I wish to have this plugin? I am not too good with working with the terminal. If I have donwloaded the RJavaPlugin****.jar file, is there a manual way I can make sure Eclipse has access to it this plugin? Thanks again... –  Apr 15 '13 at 22:35
  • For instance, I tried to drag and drop it into my JAVA project in the Package Explorer, but the imports at the top of the code "import org.rosuda.JRI.REXP" etc still "could not be resolved." –  Apr 15 '13 at 22:37
  • If your using Eclipse, add the JAR to your project build path by right clicking the project, then Properties->Java Build Path->Add External JARs. Dialog box will pop open, browse to your JAR, select, and click OK to finish. – Perception Apr 15 '13 at 22:52
  • I had actually tried that right before your comment! However, it does not appear in the Package Explorer. I think that pushing "OK" at the end does not do anything....? –  Apr 15 '13 at 22:58
  • Basically, after following that procedure, I see the RJavaPlugin_...jar file under my "Referenced Libraries" tab under my project folder. Is this all that is needed? And if so, do you have an idea why my imports still cannot be resolved? –  Apr 15 '13 at 23:16
  • I don't know what RJavaPlugin.jar is. Afaik the JAR you should be importing is JRI-0.8-4.jar. See screenshot attached to my answer. – Perception Apr 15 '13 at 23:22
  • I was adding the wrong .jar file. Thanks so much! It helped! Thanks for helping me for the past hour!! :o) –  Apr 15 '13 at 23:29
  • No problem. If my answer helped you please feel free to accept it. And good luck with the project. – Perception Apr 15 '13 at 23:36
  • Sorry, I jumped the gun. There was no running time error, but there was a compile error "java.lang.UnsatisfiedLinkError: no jri in java.library.path". My package explorer looks just like the one in your screen shot, except I have "JRI-1.jar" under my "Referenced Libraries"... –  Apr 15 '13 at 23:42
  • @StellaJ - thats a runtime error (not compile). You might want to read up [here](http://binfalse.de/2011/02/talking-r-through-java/) and [here](http://www.rforge.net/JRI/) on how to completely setup the JRI environment. In any case, please post separate questions for any follow-up issues you have, as this comment chain is getting pretty long. – Perception Apr 16 '13 at 00:00
  • I agree this has been a long thread. Thanks again for your help! –  Apr 18 '13 at 02:43
  • Sorry, I just realized how to "accept" an answer, and so I did... :) –  Apr 18 '13 at 21:43
0

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.

Community
  • 1
  • 1
Ciarán Tobin
  • 7,306
  • 1
  • 29
  • 45