R, Java, rJava and macOS adventures

R, rJava, Java and macOS is a mess. In fact, Java itself is a mess when it comes to macOS. First of all, you can have different Java installations: Oracle based or system based.

# this is the place where Java Framework resides

# this is the place where Oracle based Java will be installed

Apple’s layout is quite different comparing to what you expect from typical JDK installation.

tree -d
├── Frameworks -> Versions/Current/Frameworks
├── Resources -> Versions/Current/Resources
└── Versions
    ├── A
    │   ├── Commands
    │   ├── Frameworks
    │   │   ├── JavaNativeFoundation.framework
    │   │   │   ├── Resources -> Versions/Current/Resources
    │   │   │   └── Versions
    │   │   │       ├── A
    │   │   │       │   ├── Resources
    │   │   │       │   │   └── English.lproj
    │   │   │       │   └── _CodeSignature
    │   │   │       └── Current -> A
    │   │   └── JavaRuntimeSupport.framework
    │   │       ├── Resources -> Versions/Current/Resources
    │   │       └── Versions
    │   │           ├── A
    │   │           │   ├── Resources
    │   │           │   │   └── English.lproj
    │   │           │   └── _CodeSignature
    │   │           └── Current -> A
    │   ├── JavaPluginCocoa.bundle
    │   │   └── Contents
    │   │       ├── MacOS
    │   │       ├── Resources

Anyway, if you want to configure R such way it works properly with JVM, you have to make some tweaking. First of all, you have to play with JAVA_HOME. In macOS you have this special command: java_home

> /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    1.8.0_11, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
    1.8.0_05, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home


# if you run it without any switches it will show currently used Java

so far, so good. If you tree this one, you will see that structure is quite similar to other systems (e.g. Linux, Windows).

tree -d `/usr/libexec/java_home`
├── bin
├── db
│   ├── bin
│   └── lib
├── include
│   └── darwin
├── jre
│   ├── bin
│   └── lib
│       ├── applet
│       ├── cmm
│       ├── ext
│       ├── fonts
│       ├── images
│       │   └── cursors
│       ├── jfr
│       ├── jli
│       ├── management
│       ├── security
│       └── server

However, if you try to use JDK with R, there is a small surprise behind the corner. To get Java running in R, you call javareconf as follows

sudo R CMD javareconf

But, there is a small but. javareconf assumes that whenever you JAVA_HOME matches “/Home$” using grep, it points to system based Java installation – instead of Oracle’s one. This makes things weird. Half of your settings point to JDK and other half to JVM installed by Apple. Things can go wrong, and will.

To overcome this issue, you need to create fake Java home directory – without “/Home” in the name.

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

then, you need to export it as JAVA_HOME

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

and run javareconf with settings allowing to compile the code

sudo R CMD javareconf \
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"

Now, you are able to test whether rJava compiles fine. Download rJava package and try to configure it manually (outside R) from sources.

rJava can be downloaded here: 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

if it passes, start R and install package from sources

> 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 properly in a first place. It might be needed to update file


if you want to edit the file, make sure to use some editor you prefer (e.g. VI)

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

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





rJava requires XQuartz for compilation, make sure to install it as well: https://www.xquartz.org

Comments (6)

LuisJune 9th, 2017 at 3:11 am

Hi. Thanks for posting some ideas, I’ve trying to install that for quite some time

I just tried your solution and didn’t work though

If I do
sudo `/usr/libexec/java_home`/../Info.plist
I get
sudo: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/../Info.plist: command not found

If I anyways try install.packages(“rJava”, type=”source”)
I get

clang: error: unsupported option ‘-fopenmp’
make[2]: *** [libjri.jnilib] Error 1
make[1]: *** [src/JRI.jar] Error 2
make: *** [jri] Error 2
ERROR: compilation failed for package ‘rJava’

Inside my config.log I get
configure:2890: checking for ccache /usr/local/opt/gcc/bin/gcc-7 option to accept ISO C89
configure:2953: ccache /usr/local/opt/gcc/bin/gcc-7 -c -Wall -g -O2 conftest.c >&5
configure:2953: $? = 0
configure:2966: result: none needed
configure:2996: checking how to run the C preprocessor
configure:3027: ccache /usr/local/opt/gcc/bin/gcc-7 -E conftest.c
configure:3027: $? = 0
configure:3041: ccache /usr/local/opt/gcc/bin/gcc-7 -E conftest.c
conftest.c:9:10: fatal error: ac_nonexistent.h: No such file or directory
compilation terminated.
configure:3041: $? = 1
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME “rJava”
| #define PACKAGE_TARNAME “rjava”
| #define PACKAGE_VERSION “0.8”
| #define PACKAGE_STRING “rJava 0.8”
| #define PACKAGE_BUGREPORT “Simon.Urbanek@r-project.org”
| #define PACKAGE_URL “”
| /* end confdefs.h. */
| #include
configure:3066: result: ccache /usr/local/opt/gcc/bin/gcc-7 -E
configure:3086: ccache /usr/local/opt/gcc/bin/gcc-7 -E conftest.c
configure:3086: $? = 0
configure:3100: ccache /usr/local/opt/gcc/bin/gcc-7 -E conftest.c
conftest.c:9:10: fatal error: ac_nonexistent.h: No such file or directory
compilation terminated.

any ideas? thanks anyways!

michalJune 9th, 2017 at 8:01 am

Just make sure what version of JDK do you have:

ls -la /Library/Java/JavaVirtualMachines/

or call

/usr/libexec/java_home -V

In case of JDK you should take a look here: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html and download most recent one.

And make sure to open Info.plist file with an editor you like (e.g. vi, emacs, TextMate).

LuisJune 9th, 2017 at 3:12 am

BTW, you have a typo

JAVA=${JAVA_HOME/bin/java \

JAVA=${JAVA_HOME}/bin/java \

michalJune 9th, 2017 at 7:59 am

Thanks for spotting it!


escJune 15th, 2017 at 1:39 am

I got the same error as Luis after following the steps as well

RobertoJune 24th, 2017 at 1:27 pm


I also got the same error message in R

clang: error: unsupported option ‘-fopenmp’
make[2]: *** [libjri.jnilib] Error 1
make[1]: *** [src/JRI.jar] Error 2
make: *** [jri] Error 2
ERROR: compilation failed for package ‘rJava’
* removing ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava’

Die heruntergeladenen Quellpakete sind in
In install.packages(“rJava”, type = “source”) :
Installation des Pakets ‘rJava’ hatte Exit-Status ungleich 0

Please, let me know if you find any solution. Thanks!

Leave a comment

Your comment