R, Java, rJava and macOS adventures

For updated info (for R3.4) take a look HERE.

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
/System/Library/Frameworks/JavaVM.framework

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

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

/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home

# if you run it without any switches it will show currently used Java
/usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home

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`
/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/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_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"

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
./configure

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

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

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:

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

to

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

Note!

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

Comments (12)

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
#include
^~~~~~~~~~~~~~~~~~
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
#include
^~~~~~~~~~~~~~~~~~
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).

michalAugust 10th, 2017 at 2:22 pm

Take a look here: http://www.owsiak.org/?p=3718

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!

Fixed

escJune 15th, 2017 at 1:39 am

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

michalAugust 10th, 2017 at 2:22 pm

Take a look here: http://www.owsiak.org/?p=3718

RobertoJune 24th, 2017 at 1:27 pm

Hello,

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
‘/private/var/folders/rb/tb9pmxkj5wn529v73bdfsh100000gn/T/RtmpMipMTt/downloaded_packages’
Warnmeldung:
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!

michalAugust 10th, 2017 at 2:22 pm

Take a look here: http://www.owsiak.org/?p=3718

SangJuly 10th, 2017 at 12:04 am

Hi,

I am also getting the following error messages:

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 ‘/Users/slee/Library/R/3.4/library/rJava’
Warning in install.packages :
installation of package ‘rJava’ had non-zero exit status

Any update on how to solve this error? Thanks!

michalJuly 10th, 2017 at 2:52 am

To be honest, it’s hard to say. I can’t reproduce this kind of issue. All I can say is that it works for me, and I am using version:

clang –version
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix

It seems I can’t help you much here :(

michalAugust 10th, 2017 at 2:21 pm

Take a look here: http://www.owsiak.org/?p=3718 – this one should fix the issue

Leave a comment

Your comment