1

I recently updated R to 3.4 and RStudio to its RStudio v1.0.143 Preview so that it's compatible with the image updates & plots for R, but now I'm having an issue with rJava when trying to load it or packages that require it. It's as follows:

Loading required package: 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.4/Resources/library/rJava/libs/rJava.so': dlopen(/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
Referenced from: 
 /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so
  Reason: image not found
Error: package ‘rJava’ could not be loaded

I am presently running MacOSX 10.12.4 Sierra

Any help on how to fix this is great appreciated.

5
  • 1
    You may need to reinstall it from source, see the R 3.4.0 NEWS file. Commented Jun 6, 2017 at 0:47
  • I've reinstalled Java, what else would I reinstall from source?
    – Drew
    Commented Jun 6, 2017 at 1:12
  • The rJava package may need reinstallation as R 3.4.0 changed how .C() and .Fortran() calls are resolved. That is in the NEWS file I pointed you to. Commented Jun 6, 2017 at 1:13
  • install from outside of rstudio
    – Phi
    Commented Jun 6, 2017 at 3:22
  • Uninstalled it, installed it using the local (IA) R Cran in base R (not RStudio), still getting precisely the same error.
    – Drew
    Commented Jun 6, 2017 at 3:43

2 Answers 2

2

As already noted by @mko, the mix of macOS/R/rJava is mess, albeit a very useful one. I've been wrangling with this issue over several releases of each, and no matter how much care you take it seems it bites again periodically. That said, there are various things to bear in mind:

  • You'll require a JDK installation, not just a JRE installation.
  • If not using the deprecated Apple version of Java 1.6, R's javareconf script doesn't handle the location of libjvm.dylib correctly.
  • It's best to use a recent JDK version to benefit from the latest security enhancements.
  • R's javareconf script fails to reconfigure R correctly after upgrading to a new Java version; it requires a reinstallation of R to do it properly.

So, bearing these in mind:

  • Install/update JDK.
  • It's a good idea to set JAVA_HOME correctly for whichever JDK installation you plan to use (generally in ~/.bashrc or ~/.bash_profile).
  • Install XQuartz if not yet installed (as @mko mentions).
  • (Re)install R (to allow correct R configuration against new Java installation.
  • Run javareconf script with custom parameters to let it configure for the modern JDK.
  • Link Java runtime to R's library folder to it can find it.

The custom call to javareconf is just a few lines at the terminal: Firstly, make sure JAVA_HOME is set as required:

# Make sure JAVA_HOME is set as required.
export JAVA_HOME=${JAVA_HOME:-$(/usr/libexec/java_home)}
# Obtain location of Java runtime library.
export LIBJVM=$(find "${JAVA_HOME}" -name 'libjvm.dylib')
# Reconfigure R using custom call to javareconf script.
R CMD javareconf JAVA_LIBS="${LIBJVM}" JAVA_LD_LIBRARY_PATH="${LIBJVM}" JAVA_CPPFLAGS="'-I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin -I$(dirname "${LIBJVM}")'"
# Link Java runtime to R library folder so it can be found.
ln -fs "$LIBJVM" /Library/Frameworks/R.framework/Libraries

Now you should be able to launch R and install rJava ok.

If you're using a Homebrew installation of R instead (needs to be installed with the --with-java option), use this line to link the Java runtime instead:

ln -fs "$LIBJVM" "$(brew --prefix r)/lib"

The process requires admin permissions on macOS (unless R is installed via Homebrew). The R package installer gives admin permission to the various files, so sudo isn't needed, luckily. For reference I've also recently written a brief article on this issue.

2
  • Any idea why when rJava it unlinks from the original home directory? It seems like when I update any sort of packages which rJava depends on, it unbinds.
    – alpha
    Commented Mar 14, 2019 at 13:36
  • For recent versions of Java/R/rJava this issue should now be resolved, and you shouldn't need to use this fix any more. I'm currently using R 3.5.2 with rJava 0.9-10, and it works without issue for me using Java Development Kit 8/10/11/13.
    – stan
    Commented Mar 15, 2019 at 14:51
0

rJava, R, Java and macOS is a mess.

What you can do is to try rebuild rJava from the sources.

Make sure to reconfigure Java. Due to the fact javareconf makes lots of strong assumptions when it comes to macOS and JAVA (one of these is, that whenever you have "Home" word, in JAVA_HOME variable, it will assume you want to use System wide Java - not quite good idea), you need to:

  • create "artificial" Home inside JDK, e.g.

    cd /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents ln -s Home RHome

  • export RHome as JAVA_HOME

    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/RHome

Run javareconf with loots of switches that will make gcc with JNI based code compilation

`sudo R CMD javareconf JAVA_HOME=${JAVA_HOME} JAVA=${JAVA_HOME/bin/java JAVAC=${JAVA_HOME}/bin/javac JAVAH=${JAVA_HOME}/bin/javah JAR=${JAVA_HOME}/bin/jar JAVA_LD_LIBRARY_PATH=${JAVA_HOME}/jre/lib/server JAVA_CPPFLAGS="-I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin"`

Download rJava package and try to rebuild it manually (outside R)

https://cran.r-project.org/web/packages/rJava/index.html

> curl https://cran.r-project.org/src/contrib/rJava_0.9-8.tar.gz -o rJava_0.9-8.tar.gz
> tar zxf rJava_0.9-8.tar.gz
> cd rJava
> ./configure

if it passes, start R (in the same console where you have set env)

> install.packages("rJava", type="source")

Note that with messy Java installations (Apple's JREs, Oracle's JDKs) you might need to play with setting up Java env propperly in a first place. It might be you will need some "dirty" hacks where you link javac, javah and jar from JDK inside JRE. You will know when you need to do it once ./configure complains about missing commands.

It also might be needed to update file

`/usr/libexec/java_home`/../Info.plist

and make sure your JVM can handle JNI. You need to change:

<key>JVMCapabilities</key>
<array>
  <string>CommandLine</string>
</array>

to

<key>JVMCapabilities</key>
<array>
  <string>CommandLine</string>
  <string>JNI</string>
</array>

After some struggle, you might get it working fine. However, it's really system dependent. A lot depends on what version of R you have, whether you have JDK installed or not and what is your default JVM.

Note!

rJava requires XQuartz for compilation, make sure to install it as well

https://www.xquartz.org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.