Setting up Java R Interface (JRI) on Windows

2020-02-11 08:37发布

问题:

I have followed guides on the web and looked at questions on stackoverflow but I am still not able to set up JRI on Windows.

Here are the steps I have taken:

1) Install R

2) Execute install.packages("rJava") in R to install rJava

3) Create a R_HOME system variable with value C:\Program Files\R\R-3.1.2

4) Add the following to my PATH variable: C:\Program Files\R\R-3.1.2\bin\x64;C:\Users\USERNAME\Documents\R\win-library\3.1\rJava\jri\x64

5) Added the JRI.jar (located in C:\Users\USERNAME\Documents\R\win-library\3.1\rJava\jri) as a dependency in IntelliJ

6) Set the VM options in IntelliJ to -Djava.library.path=C:\Users\USERNAME\Documents\R\win-library\3.1\rJava\jri\x64

7) Start R and execute library("rJava")

8) Run my program to test if everything is set up

Get the following error:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000006c726096, pid=5860, tid=6932
#
# JRE version: Java(TM) SE Runtime Environment (8.0_31-b13) (build 1.8.0_31-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.31-b07 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [R.dll+0x26096]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\USERNAME\Documents\Project\hs_err_pid5860.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

If I remove the VM option so that java.library.path isn't specified then I get the following 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
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1865)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1119)
    at org.rosuda.JRI.Rengine.<clinit>(Rengine.java:19)
    at com.coagsm.cs.fyp.Test.<clinit>(Test.java:11)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:260)

回答1:

How you instantiating Rengine?

It should be something like this:

Rengine rengine = new Rengine(args, false, null);

where, the args could be from your main method.

Let me know if this works!



回答2:

Latest (2018) for me was to do this :-

export R_HOME=$(R RHOME)

Then compile this:-

String[] enginargs = {"--no-save"};
REngine engine = org.rosuda.REngine.JRI.JRIEngine.createEngine(enginargs, null, false);

try {
    engine.parseAndEval("print(\"Hello from R\");");
}
catch(Exception e ) {
    System.err.println("OOps:-" + e.getMessage());
}
finally {
    engine.close();
}

Then when running the java either set the LD_LIBRARY_PATH to enclude JRI or set the -Djava.library.path eg

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

or

java -Djava.library.path=/usr/local/lib/R/site-library/rJava/jri/ MyMainClass


标签: java r rjava jri