Today I played a bit with JRI as part of rJava, a Java-R-interface. Here you can learn how to setup for Debian/Ubuntu/akins.

Installation

Assuming you have a running version of Java and GNU’s R, you have to install r-cran-rjava :

aptitude install r-cran-rjava

Shell environment

To talk to R through Java you have to specify three more environmental variables. First of all you need to publish you R installation path, my R is found in /usr/lib64/R :

export R_HOME=/usr/lib64/R

If you didn’t or the path is wrong you’ll fall into trouble:

R_HOME is not set. Please set all required environment variables before running this program.

Second the $CLASSPATH needs to get an update. Precisely you have to add the archives JRIEngine.jar , JRI.jar and REngine.jar . In my case all of them can be found in /usr/lib/R/site-library/rJava/jri/ , so the $CLASSPATH should be set like that:

export CLASSPATH=.:/usr/lib/R/site-library/rJava/jri/

If the $CLASSPATH isn’t defined correctly you won’t be able to compile your Java code.

Last but not least you have to add the native JRI-library to your $LD_LIBRARY_PATH , by default this lib is located in the same directory like the jar’s:

export LD_LIBRARY_PATH=/usr/lib/R/site-library/rJava/jri/

If the $LD_LIBRARY_PATH isn’t proper you’ll experience errors like this:

Cannot find JRI native library!
Please make sure that the JRI native library is in a directory listed in java.library.path.

java.lang.UnsatisfiedLinkError: no jri in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
        at java.lang.Runtime.loadLibrary0(Runtime.java:823)
        at java.lang.System.loadLibrary(System.java:1028)
        at org.rosuda.JRI.Rengine.<clinit>(Rengine.java:19)

To not always do the same you might write these export stuff to your .bashrc or .zshrc respectively.

Eclipse setup

Of course in Eclipse you’ll also have to define these three things. Where are the jar’s located? Add them to your libraries in Project > Properties > Java Build Path > Libraries. Instead of the $LD_LIBRARY_PATH you can set the java.library.path in Run > Run Configurations > Arguments. Add -Djava.library.path=.:/usr/lib/R/site-library/rJava/jri/ to the VM arguments (modify the path to match your criteria). The R_HOME can be published in Run > Run Configurations > Environment. Create a new variable with the name R_HOME and the value /usr/lib64/R (or an equivalent path). That’s it, see the section above to identify what went wrong if something fails.

Netbeans setup

Two of these three parts are also straight forward in Netbeans. First publish the location of the jar’s. Right-click on your project and choose Properties > Libraries. In the Compile-tab click Add JAR/Folder and search for the jar files. Next task is to adjust the library-path. Right-click on your project and choose Properties > Run. Add -Djava.library.path=.:/usr/lib/R/site-library/rJava/jri/ to the VM Options (modify the path to match your criteria). The third step is a little tricky. As far as I know there is no way to change the environment from within Netbeans, so you can’t create the variable R_HOME after Netbeans is started. In my opinion you have two options:

  1. Export the variable before starting Netbeans:
usr@srv $ export R_HOME=/usr/lib64/R
   usr@srv $ netbeans

you might want to write a wrapper script that does this step for you, or include the export in any of the resource files that are called before Netbeans starts (e.g. your .bashrc ).

  1. Change the environment from within your project. At stackoverflow you can find a workaround, but I think this is a very lousy solution..

If you have further suggestions please let me know! Meanwhile George Bull published a setup guide for Netbeans on Windows hosts. Seems to be worthy to take a look at it ;-)

Testcase

If you defined your environment properly, you should be able to utilize the REngine. I have a small script for you to test whether all things are fine:

package de.binfalse.martin;

import org.rosuda.JRI.Rengine;

public class JRItest
{
  public static void main (String[] args)
  {
    // new R-engine
    Rengine re=new Rengine (new String [] {"--vanilla"}, false, null);
    if (!re.waitForR())
    {
      System.out.println ("Cannot load R");
      return;
    }

    // print a random number from uniform distribution
    System.out.println (re.eval ("runif(1)").asDouble ());

    // done...
    re.end();
  }

}

You should be able to compile and run it, afterwards you’ll see a random number from an uniform distribution. Congratulations, well done :-P

For more information see the JRI and rJava sites at RForge.net.

Download: Java: JRItest.java (Please take a look at the man-page. Browse bugs and feature requests.)

Martin Scharm

stuff. just for the records.


28 Comments

Sebastian | Permalink | 2011-06-10 18:29:24

greetings, I have a problem big problem, when I try to execute those steps, all work fine until the IDE configuration, I explain me better, I had installed Rjava, I had copied JAR requested, I had updated the environment variables in /etc/environment and until there all works configurate correctly, I work in Netbeans but I didn’t know if I had to do any configuration in the IDE. Finally when I run the program, it appear this “cannot find system Renviron”. Really I had tried all the possible things but I couldn’t fix it. Sorry for my english, and I will appreciate any help that you can give to me for understand why is the reason of my problem. Thanks

Martin Scharm | Permalink | 2011-06-10 21:02:38

Hi Sebastian,

I added a Netbeans setup section to the article. I hope that solves some of your problems ;-)

Kind regards,
Martin

sebastian | Permalink | 2011-06-12 23:11:37

Really, thank you so much for your help, it will be so useful, now I will follow with my project.

Sergio | Permalink | 2011-07-12 10:23:41

Greetings,
Now my Java-R connection works, I can make some calculus. But I do not know how I can use R’s libraries like RODBC through Java, I tried with: re.eval("library(RODBC)") , that did not work. Can you help me to use this library? How can I test the library is actually loaded?
Thanks

Martin Scharm | Permalink | 2011-07-12 11:16:46

Hi Sergio,
loading libraries works at least for me:


{% highlight java %}
re.eval (“library(datasets)”);
System.out.println (re.eval (“beaver1[1,1]”).asDouble ());
{% endhighlight %}


Maybe your library can’t be found by R itself? Where is it located? Could you please tell me the thrown error!?
Maybe changing the R library search path might help you:


{% highlight java %}
re.parseAndEval(“.libPaths(/DIR/OF/YOUR/LIB/)”);
{% endhighlight %}

Sergio | Permalink | 2011-07-12 11:44:17

When I execute within R the command library(RODBC) it works.
I just see I have /usr/local/lib/R/site-library/RODBC/libs/RODBC.so (I suppose that is the library is required).
What is the JRI.jar version you are using? I have not got the parseAndEval method.
Thanks again

Martin Scharm | Permalink | 2011-07-12 12:21:44

Ok, sry, I just copied&pasted the code from another project. Unfortunately I’m not using org.rosuda.JRI.Rengine at this point. Sorry for that.
Let’s continue this conversation via mail ;-)

Sergio | Permalink | 2011-07-13 09:39:24

OK, I will appreciate that.
But I am interested in knowing about which jar library you are using, I may change it.
Can you know my mail or I would publish it?

Martin Scharm | Permalink | 2011-07-13 10:57:31

If you provided a correct mail address you should find my mail in your inbox. Otherwise choose one of these methods to contact me: {{ site.url }}/contact/
I’m sure we’ll fix this issue ;-)

Btw. I’m currently using 0.9-1-1 from Debian unstable. With this version the following should work:


{% highlight java %}
re.eval(“.libPaths(/DIR/OF/YOUR/LIB/)”);
{% endhighlight %}


But let’s discuss via mail. I’ll afterwards post a summary here.

Santiago | Permalink | 2011-08-09 12:00:40

Greetings,

I am trying to use JRI to programming JAve with R. I am testing a simple program but i have a problem with the REngine constructor. The program exit with the value 10, without exception. I have followed the steps before but i cant fix the problem.

Thanks.

Martin Scharm | Permalink | 2011-08-09 12:09:20

Hi Santiago,

it’s hard for me to guess what’s wrong. Please send me your code and I’ll try to help you.

sanky | Permalink | 2011-12-09 16:13:35

Is it possible to invoke r functions or embedding r charts inside jasper’s ireport

Hui YAN | Permalink | 2012-02-18 16:50:59

It really help. Thank you so much.

jesslim | Permalink | 2012-02-25 17:00:50

hi,
i hv the problem for runing this
what mean Export the variable before starting Netbeans:



{% highlight bash %}
usr@srv $ export R_HOME=/usr/lib64/R
usr@srv $ netbeans
{% endhighlight %}



write at where?

actually my task is to write a java programing that will produce few value. Then i need to pass that value to R. then R will do some process eg sum the value with one. last pass it back to java. How can it work???

Martin Scharm | Permalink | 2012-02-25 17:15:05

Hi sanky, unfortunately I don’t know jasper and so I don’t have answers to such specific questions, please try to ask for support at jasper.

Martin Scharm | Permalink | 2012-02-25 17:20:11

Hi jesslim,

if you’re working on Linux/Unix just open a terminal and export the variable with



{% highlight bash %}
export R_HOME=/usr/lib64/R
{% endhighlight %}



Afterwards you can start Netbeans in the same session with the command



{% highlight bash %}
netbeans
{% endhighlight %}



If you’re using Windows I’m sorry, but I’m not able to help you. I’m sure there is some possibility to set this variable in your user-profile, but I recommend to ask someone who is more familiar with Windows ;-)

Kind regards,
Martin

Mohan | Permalink | 2012-06-14 11:58:54

The reply is late. I am trying to investigate the best way to run simulations on my socket server with varying distributions of bytes. It looks like r-java can do it instead of expensive load runners.

Do you know of anyone who is doing it ?I asked the r-help list but they disregard generic questions like this.

Yogalakshmi | Permalink | 2012-11-05 09:09:03

Dear George,
The post of Using JRI , R with NetBeans in Windows was an excellent guide. I am using Eclipse IDE and trying to call R via a Java program using JRI.
I have gone through your post and tried to replicate the same thing in Eclipse IDE. I am yet to get a successful output out of the java program (TestR.java). Here is what I did:
1. Set R_Home variable => R_HOME = D:\Program Files\R\R-2.14.0
2. Set PATH variable => PATH = D:\Program Files\R\R-2.14.0\bin\x64;(contains R.dll) D:\Program Files\R\R-2.14.0\library\rJava\jri; (containsJRI.dll,JRI.jar,JRIEngine, REngine), D:\Program Files\R\R-2.14.0\library\rJava\jri\x64 (contains JRI.dll)
3. Added -Djava.library.path=D:\Program Files\R\R-2.14.0\library\rJava\jri\ to the VM arguments, and I executed the program and it gave the following error:
“Could not find the main class: Files\R\R-2.14.0\library\rJava\jri. Program will exit. “

Massimo | Permalink | 2012-12-20 12:36:38

Hi,
is there a way to get error messages from R back?

- CODE EXAMPLE:
Rengine rEngine = new Rengine(new String [] {“–vanilla”}, false, null);
rEngine.eval(“library(xts)”);
rEngine.assign(“arrayValue”, DOUBLE DATA);
rEngine.assign(“arrayDates”, DOUBLE DATA);
rEngine.eval(“test = xts(arrayValue, order.by=arrayDates)”);

The last statement not only takes 1 minute to run, it also seems to fail (no test variable is created)
-> Is there a way to get some log information?

Thank you and best regards!

Neil L | Permalink | 2013-02-13 09:49:21

Hi,

I’m trying to do this in DrJava but I believe I’m getting stuck with setting the R_HOME environmental variable in DrJava. Does anybody know how to do this?

Thanks

Mithil | Permalink | 2013-02-24 18:39:46

There’s an eclipse plugin that helps set up the R Java environment at http://www.studytrails.com/RJava-Eclipse-Plugin/.

Swarga Bera | Permalink | 2013-09-12 14:59:12

This is not working in my windows machine. It throws exception

Cannot find JRI native library!
Please make sure that the JRI native library is in a directory listed in java.library.path.

java.lang.UnsatisfiedLinkError: no jri in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1709)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1030)
at org.rosuda.JRI.Rengine.(Rengine.java:9)
at com.test.maintest.main(maintest.java:9)

How can i solve it.My R global path is also right.

Martin Scharm | Permalink | 2013-09-12 20:30:43

Unfortunately I’m at war with Windows. Thus, the only advise I have for you is switching to a proper operation system ;-)
However, I hope you can fix this issue. If you found a solution please post it to help other Windows users.

icesource | Permalink | 2013-11-05 14:45:42

@Swarga Bera
For windows operating system, I would suggest to check on the environment variable of the windows operating system.
I believe there is something called PATH
and jri.dll should be searchable by the environment variable.

after changing the environment variable, do not forget to restart the machine.

palex | Permalink | 2014-03-28 14:43:50

hello

i m using this configuration but when i m launching R its stop immediatly with exit code 2

i have set up -Djava.library.path correctly because before setup this, i have the error that jri.dll coulnd’nt be found.

now have done this:



{% highlight java %}
Rengine.DEBUG = 5;

System.out.println(“Starting Rengine..”);
System.out.println(“R_HOME =” + System.getenv(“R_HOME”));
System.out.println(“java.library.path =” + System.getProperty(“java.library.path”));
Rengine re = new Rengine(new String[] { “–vanilla” }, false, null);
if (!re.waitForR()) {
System.out.println(“Cannot load R”);
return;
}
{% endhighlight %}



and when i m running the programm, on line Rengine re = new Rengine(new String[] { "--vanilla" }, false, null);
it stop with exit code 2, in the console outpu i have :



{% highlight java %}
Starting Rengine..
R_HOME =D:\Users\pmancaux\Desktop\Dev\R\GNU_R\R-3.0.2\bin
java.library.path =D:\Users\pmancaux\Desktop\Dev\R\GNU_R\R-3.0.2\library\rJava\jri
Starting R…
{% endhighlight %}



i don t find any solution on internet! please help me! thanks a lot

Palex

Irene | Permalink | 2014-04-08 19:14:33

same problem as Palex here…. everything used to work perfectly fine and now application crashes when i set up the Rengine (Rengine re=new Rengine(args, false, new TextConsole()); but i’ve used all syntax possible…)

no idea… its driving me nuts….

any help is dearly appreciated.

Irene | Permalink | 2014-04-08 21:30:27

ok, me again, sorry for mass posting… i solved previous issue. I updated everything (JRI and R) and it worked. Updating only JRI won’t do the trick. Anyway….
Cheers :)

Martin Scharm | Permalink | 2014-04-08 21:38:27

Hi Irene, thank you very much for coming back with the fix! :)

Post a comment

read more about submitting comments