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 (22)

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

pranathiSeptember 28th, 2017 at 1:10 am

“cd /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents
ln -s Home RHome
then, you need to export it as JAVA_HOME ”

I am following the same step by step instruction as you mentioned. But I get an error saying ” Permission denied” at the above step in creating Java home directory

michalSeptember 28th, 2017 at 6:59 am

You might need to use “sudo”

sudo ln -s Home RHome

pranathiSeptember 28th, 2017 at 6:54 pm

Hi ,

Thanks for answering my previous question . However when I try to confifure rJava manually , I get the following error

“`
checking JNI data types… 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.
“`

michalSeptember 29th, 2017 at 1:54 pm

I think this is related to mixed JDKs – Apple’s and Oracle’s :( At least this was the issue in my case.
You need to make sure to create artificial name for JDK that is not “Home”.

HristoOctober 3rd, 2017 at 10:34 am

Hi Michal,

I am struggling quite a lot with this Java mess… At least the good thing is that I have learned quite a lot for my Mac :)
I am hoping you can take a look and give some advice :) Thanks a lot!

Log:
https://drive.google.com/open?id=0B7wVlzNi44EUMkpBYWowdTcwaW8

Info.plist:
https://drive.google.com/open?id=0B7wVlzNi44EUXzNiNUE4b0p3YjQ

Regards,
Hristo

michalOctober 3rd, 2017 at 2:43 pm

It’s hard to judge what exactly is the source of the issue here. I’d suggest to play with JAVA_HOME pointing to JRE. And, as you can see, your R configuration mixes two Java releases – one from Oracle and one from Apple.

LilyOctober 18th, 2017 at 8:21 pm

Hi michal,

I’m having the same problem as pranathi about One or more JNI types, I think you already solved this in your tutorial by making the RHome instead of Home. So how do I make “artificial name for JDK that is not “Home”” again?

michalOctober 18th, 2017 at 9:33 pm

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

zadig_mtDecember 7th, 2017 at 5:41 am

Hi Michal

Thanks for the post

Unfortunatelly I’m stuck on this:

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_LIBS=”-L${JAVA_HOME}/lib/server -ljvm” \
> JAVA_CPPFLAGS=”-I${JAVA_HOME}/../include -I${JAVA_HOME}/../include/darwin”
Password:
/Library/Frameworks/R.framework/Resources/bin/javareconf: line 66: -ljvm: command not found
/Library/Frameworks/R.framework/Resources/bin/javareconf: line 66: -I/jre/../include/darwin: No such file or directory
*** JAVA_HOME is not a valid path, ignoring
Java interpreter : /jre/../bin/java
/Library/Frameworks/R.framework/Resources/bin/javareconf: line 105: /jre/../bin/java: No such file or directory

*** Java interpreter doesn’t work properly.

iMac-de-Fernando:~ fernandovieira$ export JAVA_HOME=$(/usr/libexec/java_home -v 9.0.1)iMac-de-Fernando:~ fernandovieira$ 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_LIBS=”-L${JAVA_HOME}/lib/server -ljvm” \
> JAVA_CPPFLAGS=”-I${JAVA_HOME}/../include -I${JAVA_HOME}/../include/darwin”
Password:
/Library/Frameworks/R.framework/Resources/bin/javareconf: line 66: -ljvm: command not found
/Library/Frameworks/R.framework/Resources/bin/javareconf: line 66: -I/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/../include/darwin: No such file or directory
Java interpreter : /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/../bin/java
/Library/Frameworks/R.framework/Resources/bin/javareconf: line 105: /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/../bin/java: No such file or directory

*** Java interpreter doesn’t work properly.

Could you help me?

michalDecember 7th, 2017 at 10:11 am

It looks like Java 9 changed the layout (they have separated JRE and JDK completely). I will get back to this article and prepare something like: “R 3.4, JDK 9 and super mess in macOS”. Stay tuned.

Leave a comment

Your comment