zhang
zhang

Reputation:

Question about JRI error

I try to run my R script in Java, thus I installed JRI. and run the example, I am using Eclipse on 64 bits windows 7. part of the example code is as follows:

    public static void main(String[] args) {
        System.out.println("Creating Rengine (with arguments)");
 Rengine re=new Rengine(args, false, null);
        System.out.println("Rengine created, waiting for R");
 if (!re.waitForR()) {
            System.out.println("Cannot load R");
            return;
        }

However, everytime I run it. it teminated after print out "Creating Rengine (with arguments)" never successfully print out "Rengine created, waiting for R"

I do not know what is right argument to input, I have tried to add "--no-save" under the Program arguments of eclipse run configuration, but still does not help.

any suggestions? Many thanks

Upvotes: 16

Views: 24204

Answers (5)

Yoichi
Yoichi

Reputation: 12261

Ref: http://www.rforge.net/JRI/

  • R_HOME must be set correctly
  • (Windows): The directory containing R.dll must be in your PATH
  • (Mac): Well, it's a Mac, so it just works ;).
  • (unix): R must be compiled using --enable-R-shlib and the directory containing libR.so must be in LD_LIBRARY_PATH. Also libjvm.so and other dependent Java libraries must be on LD_LIBRARY_PATH.
  • JRI library must be in the current directory or any directory listed in java.library.path.
  • Alternatively you can specify its path with -Djava.library.path= when starting the JVM. When you use the latter, make sure you check java.library.path property first such that you won't break your Java.
  • Depending on your system, the R verison and other features you want to use, you may have to set additional settings such as R_SHARE_DIR, R_INCLUDE_DIR and R_DOC_DIR.

My own notes:

Installing R on Mac:

Ref: http://cran.r-project.org/mirrors.html Ref: http://cran.r-project.org/bin/macosx/RMacOSX-FAQ.html

Downloaded MacOSX binary R-2.13.0.pkg from http://cran.csiro.au/


Installing Java Interface for R (JIR) on Mac OS X

Ref: http://www.rforge.net/JRI/

JIR is now a part of rJava.

> install.packages("rJava")
--- Please select a CRAN mirror for use in this session ---
trying URL 'http://cran.csiro.au/bin/macosx/leopard/contrib/2.13/rJava_0.8-8.tgz'
Content type 'application/x-gzip' length 769006 bytes (750 Kb)
opened URL
==================================================
downloaded 750 Kb


The downloaded packages are in
    /var/folders/ZZ/ZZ7tAIJsFU8FDfPPp7H53++++TM/-Tmp-//RtmpCHz9Mh/dow

> library(rJava)
> .jinit()
> s <- .jnew("java/lang/String", "Hello World!")
> print(s)
[1] "Java-Object{Hello World!}"

Running JRI example Java with Eclipse on Mac OS X

Copied the rJava/jri/examples/rtest.java to Eclipse JRI/src/. Copied the JRI.jar from the jri directory to the JRI/lib/ and added it to the build path.

The rtest.java compiles but it did not run as a Java application.

Apparently, it needs to be run by: ./run rtest.

After examining the "run" shell script which comes with the rJava package (inside ./jri/), I have set up these in the Eclipse run configuration:

Environnment:

R_HOME = /Library/Frameworks/R.framework/Resources
PATH=/Library/Frameworks/R.framework/Resources/bin/

In addition, the example run shell script sets up the following Environment vars:

R_INCLUDE_DIR=Library/Frameworks/R.framework/Resources/include
R_SHARE_DIR=Library/Frameworks/R.framework/Resources/share
R_DOC_DIR=Library/Frameworks/R.framework/Resources/doce
JRI_LID_PATH=Library/Frameworks/R.framework/Resources/lib:/Library/Frameworks/R.framework/Resources/bin

VM argument:

-Djava.library.path=/Library/Frameworks/R.framework/Resources/library/rJava/jri/

JRI.jar is now referenced in the build path, which is:

/Library/Frameworks/R.framework/Resources/library/rJava/jri/JRI.jar. 

This automatically appears under the -cp tab in the run configuration.

Also, the run configuration requires a program argument: --save, --no-save or --vanilla.

Upvotes: 27

Swamy
Swamy

Reputation: 799

I got the same problem while working with rJava, Windows 7 64 bit.

And here is the solution.

Added -Djava.library.path="${env_var:PATH}" in eclipse -> Project -> Run As -> Run Configuration -> Arguments.

And make sure System.getProperty("java.library.path") is pointing to jri.dll

I spent lot of time on this issue, so feel free to ask any questions ;)

Upvotes: 1

Raja Nagendra Kumar
Raja Nagendra Kumar

Reputation: 790

The below error can be due to two paths not set, one is r.dll not found or/and jri.dll not found. Make sure system path includes for 32 bit systems

%R_HOME%\bin;%R_HOME%\bin\i386;%R_HOME%\library\rJava\jri\i386

Also make sure install.packages("rJava") plugin is installed from R console

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:1886)
    at java.lang.Runtime.loadLibrary0(Runtime.java:849)
    at java.lang.System.loadLibrary(System.java:1088)
    at org.rosuda.JRI.Rengine.<clinit>(Rengine.java:19)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:274)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray$1.run(CallSiteArray.java:65)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray$1.run(CallSiteArray.java:62)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallStaticSite(CallSiteArray.java:62)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:159)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
    at com.acumetric.crux.analysis.StatisticCalculator.<init>(StatisticCalculator.groovy:15)

Upvotes: 0

woddle
woddle

Reputation: 1622

I have found this question in my own struggles with R on Windows, although on Linux it just worked.

I too found I was getting "Creating Rengine (with arguments)" but nothing further.

It was finding jri.dll and R.dll, which I was able to prove:

  • jri.dll - deleted it from the current directory, which generated the error:

    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

(on copying it back, make sure to take the right 32 or 64 bit version to avoid "Can't load IA 32-bit .dll on a AMD 64-bit platform" type errors)

  • R.dll lives under the installation directory C:\Program Files\R\R-2.15.0\bin\x64 If the PATH is set incorrectly in the run.bat file (which it was) then you get the slightly different error

    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: [CURRENT DIR]\jri.dll: Can't find dependent libraries

With those two things set correctly (and no hs_pid* files being generated) I found the problem to be the lack of R_HOME in run.bat. Adding

set R_HOME=C:\Program Files\R\R-2.15.0\

fixed it, and now it all seems to work for me. I did not need to add --save, --no-save or --vanilla to the script. Also, it had 'runtest' hardcoded, so ./run.bat runtest2 will not DWYM.

Short answer: The Windows run.bat script was broken (whereas the Linux 'run' script was correct out of the box).

Upvotes: 1

Dr G
Dr G

Reputation: 4017

Chances are it's not finding jri.dll or R.dll. If you see nothing, not even an exception, the JVM is crashing. Look for files like hs_pid* which should tell you which library is the culprit.

Upvotes: 1

Related Questions