How to Load Rjava on R

Unable to load rJava on R

Use:

Sys.setenv(JAVA_HOME='...path to JRE...')

e.g.

Sys.setenv(JAVA_HOME='C:\\Program Files\\Java\\jdk1.7.0_51\\jre')

Your environment variable is wrong.

Can't load rJava on MacOS Mojave and R 3.5.2

This is the setup that works for me

  1. macOS Mojave - 10.14.3

  2. XQuartz - version 2.7.11 - https://www.xquartz.org

Sample Image


  1. R - version 5.3.2

    > R --version
    R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
    Copyright (C) 2018 The R Foundation for Statistical Computing
    Platform: x86_64-apple-darwin15.6.0 (64-bit)

    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under the terms of the
    GNU General Public License versions 2 or 3.
    For more information about these matters see
    http://www.gnu.org/licenses/.
  2. Java - 11.0.1

    > java -version
    java version "11.0.1" 2018-10-16 LTS
    Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
    Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)
  3. rJava - installed from CRAN

    > > install.packages("rJava")
    --- Please select a CRAN mirror for use in this session ---
    Fontconfig warning: ignoring UTF-8: not a valid region tag
    trying URL 'https://cloud.r-project.org/bin/macosx/el-
    capitan/contrib/3.5/rJava_0.9-10.tgz'
    Content type 'application/x-gzip' length 739259 bytes (721 KB)
    ==================================================
    downloaded 721 KB


    The downloaded binary packages are in
    /var/folders/...
    > library(rJava)
    >

Update

In your case, it looks like JDK used during installation/compilation of rJava is missing. You can tell that from the message:

dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found

Make sure this file exists:

/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib

Probably, it's missing. You have two options here: install Java 11.0.1, reconfigure R.

You can list all the available JVM installations using /usr/libexec/java_home -V

Reproducing initial issue, and fixing it

  1. Let's pretend we have removed JDK 11.0.1

    > pwd /Library/Java/JavaVirtualMachines
    > tree -L 1
    .
    |-- jdk-11.0.1.jdk~
    `-- jdk-11.0.2.jdk
    > java -version
    java version "11.0.2" 2019-01-15 LTS
    Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
    Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)
  2. Let's try to load rJava

    > R

    R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
    Copyright (C) 2018 The R Foundation for Statistical Computing
    Platform: x86_64-apple-darwin15.6.0 (64-bit)

    ...
    ...
    ...

    > library(rJava)
    Error: package or namespace load failed for ‘rJava’:
    .onLoad failed in loadNamespace() for 'rJava', details:
    call: dyn.load(file, DLLpath = DLLpath, ...)
    error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so':
    dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
    Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
    Reason: image not found
  3. Let's fix it

    > sudo R CMD javareconf
    Java interpreter : /usr/bin/java
    Java version : 11.0.2
    Java home path : /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
    Java compiler : /usr/bin/javac
    Java headers gen.: /usr/bin/javah
    Java archive tool: /usr/bin/jar

    trying to compile and link a JNI program
    detected JNI cpp flags : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin
    detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
    clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/include/darwin -I/usr/local/include -fPIC -Wall -g -O2 -c conftest.c -o conftest.o
    clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation


    JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
    Java library path: $(JAVA_HOME)/lib/server
    JNI cpp flags : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin
    JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
    Updating Java configuration in /Library/Frameworks/R.framework/Resources
    Done.
  4. It should work now

    > R

    R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
    Copyright (C) 2018 The R Foundation for Statistical Computing
    Platform: x86_64-apple-darwin15.6.0 (64-bit)

    ...
    ...
    ...

    > library(rJava)
    > quit()


Related Topics



Leave a reply



Submit