Getting rJava to work in OS X El Capitan: A non-technical guide

written in r, programming tips

I recently needed to install the XLConnect package in R, which has the dependency rJava. Unfortunately rJava is one of those R packages which is a real pain to install and required a bit of fiddling. I’ll talk you through how I installed it in OS X El Capitan (for reference, I’m currently using version 10.11.2).

First errors first: RStudio

I started by trying to install the XLConnect package in RStudio:

install.packages("XLConnect")

R threw the following error:

ERROR: dependency rJava is not available for package XLConnect
* removing /usr/local/lib/R/3.2/site-library/XLConnect
Warning in install.packages :
    installation of package XLConnect had non-zero exit status

No problem! I thought. I’ll just manually install rJava:

install.packages("rJava")

Unfortunately, this didn’t work.

configure: error: One or more JNI types differ from the corresponding native type. You may need to use non-standard compiler flags or a different compiler in order to fix this.
ERROR: configuration failed for package rJava
* removing /usr/local/lib/R/3.2/site-library/rJava
Warning in install.packages :
    installation of package rJava had non-zero exit status

I’ll be the first to admit I am no software engineer, so I had no idea what this meant. Fortunately, my boyfriend is a software engineer, so I asked him what I should do.

Setting up the JDK

He had a look at the CRAN documentation for rJava and said, “Well, you need to install a JDK.” I went to Google, found the latest version of the JDK (or Java Development Kit) and installed it (at the time of this post, this was version 1.8). After installing it, I tried running install.packages("rJava") in R again. It still didn’t work. I went back to my boyfriend, and he told me I hadn’t set Java home to point at where my JDK was stored. Oh. He opened up my Fish shell interpreter and set the Java home using the following code.

/usr/libexec/java_home -v 1.8.0
echo $JAVA_HOME
set JAVA_HOME (/usr/libexec/java_home -v 1.8.0)

If you are using bash, you can use the alternative code:

/usr/libexec/java_home -v 1.8.0
echo $JAVA_HOME
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0`

As recommended by several StackOverflow posts (this, for example), we then ran the following R command that reconfigures Java.

R CMD javareconf

I excitedly opened RStudio and ran install.packages("rJava") and … it still didn’t work! What the heck?

Setting up the JDK: second attempt

After a bit more rummaging, we found out that R only works with Java 1.6. Oh great. (This is despite the instructions in the rJava documentation that indicates you can use any version from 1.2 upwards.) So I went back to Oracle’s site to download it, and it’s of course not there. We track it down on the Apple site, install it, and tried to reset Java home to point to this version.

/usr/libexec/java_home -v 1.6.0
set JAVA_HOME (/usr/libexec/java_home -v 1.6.0)
/usr/libexec/java_home

It still wouldn’t change the path, so we found this handy little Fish function on StackOverflow:

function jhome
    set JAVA_HOME (/usr/libexec/java_home $argv)
    echo "JAVA_HOME:" $JAVA_HOME
    echo "java -version:"
    java -version
end

The same user also provided an alternative script for bash:

#!/bin/bash

jhome () {
  export JAVA_HOME=`/usr/libexec/java_home [email protected]`
  echo "JAVA_HOME:" $JAVA_HOME
  echo "java -version:"
  java -version
}

You then execute the function as below:

jhome -v 1.6

Success!

I went back to RStudio for the fourth time and ran:

install.packages("rJava")

And it finally worked! I was then able to smoothly install XLConnect and extract my Excel data. Whew!