R 3.4, rJava, macOS and even more mess ;)

So, you want to have rJava (e.g. rJava_0.9-8.tar.gz) inside your fresh, new installation of R 3.4 and you are running macOS. There are bad news and good news ;)

Bad news. It will fail with default clang that comes with XCode. You need something better here, something with support for OpenMP. And you can get it following way

Make sure to take a look here as well: https://cran.cnr.berkeley.edu/bin/macosx/tools/

# make sure to create some place where you want to have it (I, personally, put stuff into ~/opt)
> mkdir ~/opt
> cd ~/opt
> curl http://releases.llvm.org/4.0.1/clang+llvm-4.0.1-x86_64-apple-darwin.tar.xz \
-o clang+llvm-4.0.1-x86_64-apple-darwin.tar.xz
> tar xf clang+llvm-4.0.1-x86_64-apple-darwin.tar.xz

Now, make sure to install gfortran. I am using version from this location: GFortran.

Once you have it, make sure to install most recent JDK from Oracle. You can find it here: JavaSE.

After you have your Java installed, make sure to do following (double check that everything is OK so far)

> R --version
R version 3.4.1 (2017-06-30) -- "Single Candle"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
http://www.gnu.org/licenses/.
> /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
    1.8.0_144, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
    1.8.0_111, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
    1.7.0_80, x86_64:	"Java SE 7"	/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

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

# Make sure to put following line inside you ~/.profile
# export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_144)
# close and re-open Terminal session. Now, you should be able to see:
> echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
/usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home

Make sure to enable your JDK for JNI

> sudo vi /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Info.plist

# make sure to replace
#<array>
#  <string>CommandLine</string>
#</array>
# with
#<array>
#  <string>CommandLine</string>
#  <string>JNI</string>
#</array>

Now, it’s time to configure R. Make sure to run following command.

!! (note that we change JAVA_HOME to JRE) !!

> JAVA_HOME=${JAVA_HOME}/jre
> 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"

Note! It looks like R CMD javareconf doesn’t update all the flags, make sure that file: /Library/Frameworks/R.framework/Versions/3.4/Resources/etc/Makeconf contains following entries:

JAVA_LIBS="-L${JAVA_HOME}/lib/server -ljvm" \
JAVA_CPPFLAGS="-I${JAVA_HOME}/../include -I${JAVA_HOME}/../include/darwin"

You should have your Java and R linked together. It’s time to add support for clang that you have just installed few steps above.

Let’s say your clang is in your home dir. Here: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/

Make sure, to add following file (~/.R/Makevars – more info can be found here: R for Mac OS X FAQ and here: R Installation and Administration)

CLANG=/Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0
CC=$(CLANG)/bin/clang
CXX=$(CLANG)/bin/clang++
CXX1X=$(CLANG)/bin/clang++
CXXFLAGS=-I$(CLANG)/include

Also, make sure to modify file:

/Library/Frameworks/R.framework/Versions/3.4/Resources/etc/Makeconf

inside this file, make sure that line “LDFLAGS” reads

LDFLAGS = -L/usr/local/lib -L/Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/lib -lomp

You can confirm that it works by calling:

# you can confirm that by calling
> R CMD config --ldflags
-fopenmp -L/usr/local/lib 
-L/Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/lib 
-lomp -F/Library/Frameworks/R.framework/.. -framework R 
-lpcre -llzma -lbz2 -lz -licucore -lm -liconv

Now, it’s time to get rJava. Simply download it, and install it:

> curl "https://cran.r-project.org/src/contrib/rJava_0.9-8.tar.gz" -o rJava_0.9-8.tar.gz
> R CMD INSTALL rJava_0.9-8.tar.gz
* installing to library ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library’
* installing *source* package ‘rJava’ ...
...
...
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (rJava)

And you can now test it. Let’s say you have (in your working directory) following layout

utils/
└── RUsingStringArray.java

and file RUsingStringArray.java contains

package utils;

public class RUsingStringArray {
  public String [] createArray() {
    System.out.println("Creating empty array");
    return new String[0];
  }
}

You can compile it and run it in R

> javac utils/*.java
> export CLASSPATH=`pwd`
> export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_144)/jre
> R
> library(rJava)
> .jinit()
> obj <- .jnew("utils.RUsingStringArray")
> s <- .jcall(obj, returnSig="[Ljava/lang/String;", method="createArray")
> .jcall(obj, returnSig="I", method = "arrayLen", s)
Class: class [Ljava.lang.String;
[1] 0
>

If you are looking for more JNI samples, take a look here: http://jnicookbook.owsiak.org

Comments (59)

anonymousAugust 28th, 2017 at 1:58 am

Thanks heaps for the tutorial! I get an error when running CC=$(CLANG)/bin/clang saying clang: error: no input files. I’ve specified the /opt… folder correctly for :CLANG=/Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0
Any ideas?
Cheers

anonymousAugust 28th, 2017 at 9:08 am

You need to be more precise here ;) If you can provide build log, few lines above the error, or exact command you want to run, it could help to identify the root cause of your issue :)

anonymousAugust 31st, 2017 at 3:42 pm

Hi, I failed at ./configure of rJava, receiving the config.log.
Do I need an extra modification from your tutorial?

Thanks
Katsuhito

###
This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake.

It was created by rJava configure 0.8, which was
generated by GNU Autoconf 2.69. Invocation command line was

$ ./configure

## ——— ##
## Platform. ##
## ——— ##

hostname = usersMBP.home
uname -m = x86_64
uname -r = 15.6.0
uname -s = Darwin
uname -v = Darwin Kernel Version 15.6.0: Sun Jun 4 21:43:07 PDT 2017; root:xnu-3248.70.3~1/RELEASE_X86_64

/usr/bin/uname -p = i386
/bin/uname -X = unknown

/bin/arch = unknown
/usr/bin/arch -k = unknown
/usr/convex/getsysinfo = unknown
/usr/bin/hostinfo = Mach kernel version:
Darwin Kernel Version 15.6.0: Sun Jun 4 21:43:07 PDT 2017; root:xnu-3248.70.3~1/RELEASE_X86_64
Kernel configured for up to 2 processors.
2 processors are physically available.
2 processors are logically available.
Processor type: i486 (Intel 80486)
Processors active: 0 1
Primary memory available: 8.00 gigabytes
Default processor set: 275 tasks, 1361 threads, 2 processors
Load average: 1.54, Mach factor: 0.75
/bin/machine = unknown
/usr/bin/oslevel = unknown
/bin/universe = unknown

PATH: /Users/user_name/perl5/perlbrew/bin
PATH: /Users/user_name/perl5/perlbrew/perls/perl-5.22.0/bin
PATH: /usr/local/sbin
PATH: //anaconda/bin
PATH: /usr/local/bin
PATH: /usr/bin
PATH: /bin
PATH: /usr/sbin
PATH: /sbin
PATH: /opt/X11/bin
PATH: /usr/local/git/bin
PATH: /Library/TeX/texbin

## ———– ##
## Core tests. ##
## ———– ##

configure:2245: checking for gcc
configure:2272: result: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang
configure:2501: checking for C compiler version
configure:2510: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang –version >&5
clang version 4.0.1 (tags/RELEASE_401/final)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin
configure:2521: $? = 0
configure:2510: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang -v >&5
clang version 4.0.1 (tags/RELEASE_401/final)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin
configure:2521: $? = 0
configure:2510: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang -V >&5
clang-4.0: error: argument to ‘-V’ is missing (expected 1 value)
clang-4.0: error: no input files
configure:2521: $? = 1
configure:2510: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang -qversion >&5
clang-4.0: error: unknown argument: ‘-qversion’
clang-4.0: error: no input files
configure:2521: $? = 1
configure:2541: checking whether the C compiler works
configure:2563: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang -Wall -g -O2 conftest.c >&5
configure:2567: $? = 0
configure:2615: result: yes
configure:2618: checking for C compiler default output file name
configure:2620: result: a.out
configure:2626: checking for suffix of executables
configure:2633: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang -o conftest -Wall -g -O2 conftest.c >&5
configure:2637: $? = 0
configure:2659: result:
configure:2681: checking whether we are cross compiling
configure:2689: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang -o conftest -Wall -g -O2 conftest.c >&5
conftest.c:9:10: fatal error: ‘stdio.h’ file not found
#include
^~~~~~~~~
1 error generated.
configure:2693: $? = 1
configure:2700: ./conftest
./configure: line 2702: ./conftest: No such file or directory
configure:2704: $? = 127
configure:2711: error: in `/Users/user_name/Downloads/rJava’:
configure:2713: error: cannot run C compiled programs.
If you meant to cross compile, use `–host’.
See `config.log’ for more details

## —————- ##
## Cache variables. ##
## —————- ##

ac_cv_env_CC_set=
ac_cv_env_CC_value=
ac_cv_env_CFLAGS_set=
ac_cv_env_CFLAGS_value=
ac_cv_env_CPPFLAGS_set=
ac_cv_env_CPPFLAGS_value=
ac_cv_env_CPP_set=
ac_cv_env_CPP_value=
ac_cv_env_LDFLAGS_set=
ac_cv_env_LDFLAGS_value=
ac_cv_env_LIBS_set=
ac_cv_env_LIBS_value=
ac_cv_env_build_alias_set=
ac_cv_env_build_alias_value=
ac_cv_env_host_alias_set=
ac_cv_env_host_alias_value=
ac_cv_env_target_alias_set=
ac_cv_env_target_alias_value=
ac_cv_prog_ac_ct_CC=/Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang

## —————– ##
## Output variables. ##
## —————– ##

CC=’/Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang’
CFLAGS=’-Wall -g -O2′
CPP=”
CPPFLAGS=”
DEFS=”
ECHO_C=’\c’
ECHO_N=”
ECHO_T=”
EGREP=”
EXEEXT=”
GREP=”
JAVA=”
JAVAC=”
JAVAH=”
JAVA_CPPFLAGS=”
JAVA_HOME=’/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home’
JAVA_LIBS=”
LDFLAGS=”
LIBOBJS=”
LIBS=”
LTLIBOBJS=”
OBJEXT=”
PACKAGE_BUGREPORT=’Simon.Urbanek@r-project.org’
PACKAGE_NAME=’rJava’
PACKAGE_STRING=’rJava 0.8′
PACKAGE_TARNAME=’rjava’
PACKAGE_URL=”
PACKAGE_VERSION=’0.8′
PATH_SEPARATOR=’:’
R_HOME=’/Library/Frameworks/R.framework/Resources’
SHELL=’/bin/sh’
WANT_JRI_FALSE=”
WANT_JRI_TRUE=”
ac_ct_CC=’/Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang’
bindir=’${exec_prefix}/bin’
build_alias=”
datadir=’${datarootdir}’
datarootdir=’${prefix}/share’
docdir=’${datarootdir}/doc/${PACKAGE_TARNAME}’
dvidir=’${docdir}’
exec_prefix=’NONE’
host_alias=”
htmldir=’${docdir}’
includedir=’${prefix}/include’
infodir=’${datarootdir}/info’
libdir=’${exec_prefix}/lib’
libexecdir=’${exec_prefix}/libexec’
localedir=’${datarootdir}/locale’
localstatedir=’${prefix}/var’
mandir=’${datarootdir}/man’
oldincludedir=’/usr/include’
pdfdir=’${docdir}’
prefix=’NONE’
program_transform_name=’s,x,x,’
psdir=’${docdir}’
sbindir=’${exec_prefix}/sbin’
sharedstatedir=’${prefix}/com’
subdirs=”
sysconfdir=’${prefix}/etc’
target_alias=”

## ———– ##
## confdefs.h. ##
## ———– ##

/* 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 “”

configure: exit 1

anonymousAugust 31st, 2017 at 4:19 pm

Please ignore the previous comment, I forgot to install X-code command line tools.

anonymousAugust 31st, 2017 at 6:04 pm

Ok, no problem, but I can leave it here as a suggestion for other people, if you like. I can remove it as well ;)

anonymousSeptember 22nd, 2017 at 12:06 am

Hi! I’ve been having such a hard time with this I’ve gotten lost! I think my issue is near the first step — even though I’ve downloaded the latest version of Java, the command “/usr/libexec/java_home -V” in Terminal I only see “Java SE 6” (two times). I tried continuing through the steps despite this, but when I get to the ‘~/.profile’ command it says “Not allowed” and when I try the ‘echo’ part it just shows up as a blank white space. Did I do something to royally mess up everything and am beyond help now? Gaaah!

Thank you so much for your help and for posting this detailed solution — I can’t believe this is so difficult a problem to fix! :(

anonymousSeptember 22nd, 2017 at 5:51 am

Update to previous comment — I ended up installing Java 1.8 with Homebrew. For those like me less familiar with this kind of thing, when you need to add something to a file you use the command ‘sudo pico Makconf’ after navigating to the directory in which it’s located. (Same goes for ‘.profile’ — use ‘sudo pico .profile’). After figuring out that part, the error I described above went away, and I followed all of the rest of the steps. Also newbies (like me) make sure to change “user_name” to your username!! Everything looks good, checks out. Except when I get to the following command:

$ R CMD INSTALL rJava_0.9-8.tar.gz

Results of that command:

* installing to library ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library’
* installing *source* package ‘rJava’ …
** package ‘rJava’ successfully unpacked and MD5 sums checked
checking for gcc… /Users/katelyons/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang
checking whether the C compiler works… yes
checking for C compiler default output file name… a.out
checking for suffix of executables…
checking whether we are cross compiling… no
checking for suffix of object files… o
checking whether we are using the GNU C compiler… yes
checking whether /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang accepts -g… yes
checking for /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang option to accept ISO C89… none needed
checking how to run the C preprocessor… /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang -E
checking for grep that handles long lines and -e… /usr/bin/grep
checking for egrep… /usr/bin/grep -E
checking for ANSI C header files… rm: conftest.dSYM: is a directory
rm: conftest.dSYM: is a directory
yes
checking for sys/wait.h that is POSIX.1 compatible… yes
checking for sys/types.h… yes
checking for sys/stat.h… yes
checking for stdlib.h… yes
checking for string.h… yes
checking for memory.h… yes
checking for strings.h… yes
checking for inttypes.h… yes
checking for stdint.h… yes
checking for unistd.h… yes
checking for string.h… (cached) yes
checking sys/time.h usability… yes
checking sys/time.h presence… yes
checking for sys/time.h… yes
checking for unistd.h… (cached) yes
checking for an ANSI C-conforming const… yes
checking whether time.h and sys/time.h may both be included… yes
configure: checking whether /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang supports static inline…
yes
checking whether setjmp.h is POSIX.1 compatible… yes
checking whether sigsetjmp is declared… yes
checking whether siglongjmp is declared… yes
checking Java support in R… present:
interpreter : ‘/usr/bin/java’
archiver : ‘/usr/bin/jar’
compiler : ‘/usr/bin/javac’
header prep.: ‘/usr/bin/javah’
cpp flags : ‘-I/System/Library/Frameworks/JavaVM.framework/Headers’
java libs : ‘-L/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/lib/server -ljvm JAVA_CPPFLAGS=-I/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/../include -I/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/../include/darwin’
checking whether Java run-time works… yes
checking whether -Xrs is supported… yes
checking whether JNI programs can be compiled… configure: error: Cannot compile a simple JNI program. See config.log for details.

Make sure you have Java Development Kit installed and correctly registered in R.
If in doubt, re-run “R CMD javareconf” as root.

ERROR: configuration 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’

(‘user_name’ is actually my user name, changed it for the example). Sooooo… not sure what the issue is now! Is there anyway of just starting over from scratch? And followup: won’t some update happen eventually to fix this problem so should I just not worry about it and wait it out? :O

anonymousSeptember 22nd, 2017 at 8:25 am

Thanks for your comments! You are right, pico is probably better for regular users as it is way more intuitive :)

anonymousSeptember 23rd, 2017 at 4:27 pm

You need to take a look inside file “config.log”. The reasons may be very different. You can try setting JAVA_HOME to export JAVA_HOME=`/usr/libexec/java_home -v 9`. You may try to compile simple JNI code and see whether it works correctly (http://jnicookbook.owsiak.org/recipe-No-001/). It’s hard to tell without knowing what error message says :(

anonymousSeptember 23rd, 2017 at 3:26 am

No problem! Any ideas on how to proceed with the JNI compilation problem? :)

anonymousSeptember 24th, 2017 at 5:43 am

Oops sorry did not see the second comment sorry about that! I can’t figure out how to access the config.log file, but I have a sneaking suspicion it has something to do with JNI. When I execute “sudo R CMD javareconf ” I get an error that says:

warning: ‘JNI_CreateJavaVM’ is deprecated
[-Wdeprecated-declarations]
JNI_CreateJavaVM(0, 0, 0);
^
/System/Library/Frameworks/JavaVM.framework/Headers/jni.h:1937:1: note:
‘JNI_CreateJavaVM’ has been explicitly marked deprecated here
JNI_CreateJavaVM(JavaVM **pvm, void **penv, void *args);
^
1 warning generated.

So I’ll try to get working on solving that and attempting to compile simple JNI code as you suggest.

Thanks for responding and for posting this!! I can’t wait to get this figured out it’s been driving me crazy! :P

anonymousSeptember 24th, 2017 at 12:33 pm

It looks like you are mixing two JVMs. One, from Apple, and other one, from Oracle.

This is really frustrating with Java and R that these two can sometimes get mixed. What I suggest.

1. Put inside you ~/.profile something like this:

export JAVA_HOME=$(/usr/libexec/java_home -v 9)
export PATH=$JAVA_HOME/bin:$PATH

This way, you will set JVM to Oracle’s installation.

2. Make sure that ‘/usr/libexec/java_home -v 9’ points to JDK and not JRE. These are two, different, thing. JDK allows you to develop things, JRE makes it possible to run Java apps.

I hope it will help ;)

anonymousSeptember 28th, 2017 at 3:32 am

Hi Michal thanks for the great post!

I’m stuck at this part:
# Make sure to put following line inside you ~/.profile
# export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_144)

My output for: /usr/libexec/java_home -V

Matching Java Virtual Machines (3):
9, x86_64: “Java SE 9” /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
1.6.0_65-b14-468, x86_64: “Java SE 6” /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-468, i386: “Java SE 6” /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home

I’ve tried running: export JAVA_HOME=$(/usr/libexec/java_home -v 9)

but after closing and running: echo $JAVA_HOME
I dont get any response…

anonymousSeptember 28th, 2017 at 7:03 am

When you put line:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_144)

inside your ~/.profile, you will set JAVA_HOME each time you run Terminal (unless you use csh,tcsh, zsh – in that case you need to use another config file).

Also, make sure not to put ‘#’ in line as it marks the whole line as a comment.

You can, alternatively, put this line in file: my_settings_for_R.sh, and call it each time you want to do something with R

source ./my_settings_for_R.sh

anonymousOctober 2nd, 2017 at 11:05 pm

Hi Michal, hope I am not too late on the wagon… trying to learn R on my Mac and going through a course but got stuck on installing this stupid rJava thing and having been banging my head at it for more than a few days, more from complete stubbornnes if not anything else :) the good thing is that at least through following your tutorial and others have managed to learn quite a lot about working with Terminal …

Have gone through it once again in the past 10min so I can log it for you hoping you can take a look (please ignore a few errs on my side such as looking for Info.plist in a nonexistent java version)

So here is the link to the log file:
https://drive.google.com/open?id=0B7wVlzNi44EUMkpBYWowdTcwaW8

And here is the link to the Info.plit file if needed:

https://drive.google.com/file/d/0B7wVlzNi44EUXzNiNUE4b0p3YjQ/view?usp=sharing

Thanks in advance!

Regards,
Hristo

anonymousOctober 8th, 2017 at 2:26 am

Thank you for sharing this step-by-step tutorial. This issue really had me gridning my teeth. So, I’m running R 3-4 and downgraded my JDK to the same version you’re running. I got rJava to compile, but when I try to invoke it from R, I get this.

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’ve been looking around but haven’t really found any good suggestions. I’m not sure why the dylib is not loaded, wierd? Would really appriciate your help!

Best regards,

Jo

anonymousOctober 8th, 2017 at 2:40 am

Thank you for sharing this step-by-step tutorial. This issue really had me gridning my teeth. So, I’m running R 3-4 and downgraded my JDK to the same version you’re running. I got rJava to compile, but when I try to invoke it from R, I get this.

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

After that, I installed it in R as well, that left me with this…

Loading required package: rJava
Error: package or namespace load failed for ‘rJava’ in get(Info[i, 1], envir = env):
lazy-load database ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/R/rJava.rdb’ is corrupt
Error: package ‘rJava’ could not be loaded
In addition: Warning message:
In get(Info[i, 1], envir = env) : internal error -3 in R_decompress1

I’ve been looking around but haven’t really found any good suggestions. Would really appriciate your help!

Best regards,

Jo

anonymousOctober 9th, 2017 at 9:06 am

It’s really hard to find out what really happens at your machine. Just few suggestions from my side:

1. Make sure to export JAVA_HOME to exactly the same JVM as you have used for compilation
2. It looks like library from JVM is not visible – question is what version was used during R build
3. Double check to properly configure R with R config and make sure to set all the locations – R is super sensitive here :(
4. Take a look here: http://www.owsiak.org/?p=3671 Sometimes, you need to tweak Java installation such way it “tricks” R itself. R makes assumptions regarding Java location

Have fun with R!

anonymousOctober 11th, 2017 at 7:45 pm

Thank you SO much for your detailed instructions! (I tried it with Java jdk 9, and it failed horribly, but I downgraded to 8 on Mac OS Sierra, and it worked.

anonymousOctober 11th, 2017 at 8:20 pm

Thanks for your nice feedback!

anonymousOctober 16th, 2017 at 1:42 pm

Thanks for the tutorial! I’m running into an issue when trying to configure java for R:
$ JAVA_HOME=${JAVA_HOME}/jre
$ 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.jdk/Contents/Home/jre/../include/darwin: No such file or directory
*** JAVA_HOME is not a valid path, ignoring
Java interpreter : /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jre/../bin/java
/Library/Frameworks/R.framework/Resources/bin/javareconf: line 105: /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jre/../bin/java: No such file or directory

*** Java interpreter doesn’t work properly.

Any idea how to address this?

anonymousOctober 16th, 2017 at 5:44 pm

It looks like for JDK 9 there is no longer JRE inside JDK. This is, probably the reason for your error. It looks like you have to download JRE separately. Alternatively, try to use:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home

anonymousOctober 21st, 2017 at 7:05 pm

Hello,

THANKS to you blog, i managed to get may terminal.app say:
4 warnings
installing via ‘install.libs.R’ to /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (rJava)

>>> I got really happy for a few seconds :)

When going into R (3..4.2), sessionInfo() says :
> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] fr_FR.UTF-8/fr_FR.UTF-8/fr_FR.UTF-8/C/fr_FR.UTF-8/fr_FR.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached):
[1] compiler_3.4.2
>

>>> No rJava is available from R.

Do you have any idea what is could be ?

Thanks a lor

anonymousOctober 21st, 2017 at 9:51 pm

Did you load the library using “library(rJava)” ?

anonymousOctober 22nd, 2017 at 12:28 am

Hello,
here is what happen when i do so :

loaded via a namespace (and not attached):
[1] compiler_3.4.2
> library(rJava)
Erreur : package or namespace load failed for ‘rJava’:
.onLoad a échoué dans loadNamespace() pour ‘rJava’, détails :
appel : dyn.load(file, DLLpath = DLLpath, …)
erreur : impossible de charger l’objet partagé ‘/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
>
Thanks for your patience :)

anonymousOctober 23rd, 2017 at 9:14 am

Hello Michal,

Maybe i have an idea, sorry if this is stupid, i am not an expert at all.
The bash did this:
installing via ‘install.libs.R’ to /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava

So i check what is in /Library/Frameworks/R.framework/Versions/

I found this
ls:
3.4 Current

>>> My impression is that “Current” is the Current install of R while all the install was done for a 3.4…

Is it possible ?

Thanks a lot in advance
Bruno

anonymousOctober 23rd, 2017 at 9:41 pm

It looks like you have some issues with setting up library location. Try following:

otool -L /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so

and take a look whether your shared library can access libjvm

anonymousOctober 23rd, 2017 at 9:42 pm

Usually, Current points to most recent installation of software. Try this:

ls -l /Library/Frameworks/R.framework/Versions/Current

it will probably point to 3.4

anonymousOctober 23rd, 2017 at 11:54 pm

Hi Michal,

as you propose, i did the ls -l
I get this :

lrwxr-xr-x 1 root admin 3 21 oct 16:29 /Library/Frameworks/R.framework/Versions/Current -> 3.4

How can i get out of this ?

Thanks a lot!

anonymousOctober 26th, 2017 at 10:50 am

Dear Michal,

forget about my previous comment.

I figured it out, that was a stupid mistake … Now however, I get a different error when trying to install rJAva:

R CMD INSTALL rJava_0.9-9.tar.gz
* installing to library ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library’
* installing *source* package ‘rJava’ …
** package ‘rJava’ successfully unpacked and MD5 sums checked
checking for gcc… /Users/macmarco/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang
checking whether the C compiler works… yes
checking for C compiler default output file name… a.out
checking for suffix of executables…
checking whether we are cross compiling… no
checking for suffix of object files… o
checking whether we are using the GNU C compiler… yes
checking whether /Users/macmarco/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang accepts -g… yes
checking for /Users/macmarco/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang option to accept ISO C89… none needed
checking how to run the C preprocessor… /Users/macmarco/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang -E
checking for grep that handles long lines and -e… /usr/bin/grep
checking for egrep… /usr/bin/grep -E
checking for ANSI C header files… rm: conftest.dSYM: is a directory
rm: conftest.dSYM: is a directory
yes
checking for sys/wait.h that is POSIX.1 compatible… yes
checking for sys/types.h… yes
checking for sys/stat.h… yes
checking for stdlib.h… yes
checking for string.h… yes
checking for memory.h… yes
checking for strings.h… yes
checking for inttypes.h… yes
checking for stdint.h… yes
checking for unistd.h… yes
checking for string.h… (cached) yes
checking sys/time.h usability… yes
checking sys/time.h presence… yes
checking for sys/time.h… yes
checking for unistd.h… (cached) yes
checking for an ANSI C-conforming const… yes
checking whether time.h and sys/time.h may both be included… yes
configure: checking whether /Users/macmarco/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang supports static inline…
yes
checking whether setjmp.h is POSIX.1 compatible… yes
checking whether sigsetjmp is declared… yes
checking whether siglongjmp is declared… yes
checking Java support in R… present:
interpreter : ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/../bin/java’
archiver : ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/../bin/jar’
compiler : ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/../bin/javac’
header prep.: ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/../bin/javah’
cpp flags : ‘-I/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/../include/darwin’
java libs : ‘-L/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/server -ljvm’
checking whether Java run-time works… yes
checking whether -Xrs is supported… yes
checking whether JNI programs can be compiled… yes
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.
ERROR: configuration 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’

Do you have any idea on how to solve this?

Thank you in advance for your time: I am sorry to bother you but this installation of rJava is driving me crazy (it is 3 days that I am trying…).

Marco

anonymousOctober 27th, 2017 at 12:16 pm

Try to build the package outside R and look what happens there. It might be that configuration sets two different JVMs for compilation. It’s hard to say without logs :(

Just go to package dir (rJava) and call ./configure.

anonymousOctober 28th, 2017 at 1:59 pm

Hi Michal,

as you propose, i did the ls -l
I get this :

lrwxr-xr-x 1 root admin 3 21 oct 16:29 /Library/Frameworks/R.framework/Versions/Current -> 3.4

How can i get out of this ?

Thanks a lot!

anonymousOctober 28th, 2017 at 2:16 pm

This shouldn’t affect your installation. The only thing, you have to do, is to make sure that you point proper locations while building all the pieces. Unfortunately, this may vary between systems and it’s hard to guess which element doesn’t fit the rest :(

anonymousNovember 2nd, 2017 at 10:14 pm

Hi Michal,

Thank you so much for posting this! I have been trying to figure this out for the past two days straight so this guide has helped me a bunch! I am getting stuck at this part of your tutorial:
“Let’s say your clang is in your home dir. Here: /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/

Make sure, to add following file (~/.R/Makevars – more info can be found here: R for Mac OS X FAQ and here: R Installation and Administration)

CLANG=/Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0
CC=$(CLANG)/bin/clang
CXX=$(CLANG)/bin/clang++
CXX1X=$(CLANG)/bin/clang++
CXXFLAGS=-I$(CLANG)/include”

I am not sure what you mean by adding the Makevars file, I have consulted the two links you suggest and wasn’t able to find guidance on how to add the file, so I copied the file into the /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/ folder. When I try to run the CC, CXX, CXX1X, and CXXFLAGS lines of code in terminal I get the message:
“clang: error: no input files”.

If I try to reinstall the package in R I get the following message:
“1 warning generated.
clang -c -o Rinit.o Rinit.c -g -Iinclude -DRIF_HAS_CSTACK -DRIF_HAS_RSIGHAND -Wall -g -O2 -I/System/Library/Frameworks/JavaVM.framework/Headers -fno-common -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include
clang -c -o globals.o globals.c -g -Iinclude -DRIF_HAS_CSTACK -DRIF_HAS_RSIGHAND -Wall -g -O2 -I/System/Library/Frameworks/JavaVM.framework/Headers -fno-common -I/System/Library/Frameworks/JavaVM.framework/Headers
clang -c -o rjava.o rjava.c -g -Iinclude -DRIF_HAS_CSTACK -DRIF_HAS_RSIGHAND -Wall -g -O2 -I/System/Library/Frameworks/JavaVM.framework/Headers -fno-common -I/System/Library/Frameworks/JavaVM.framework/Headers
clang -o libjri.jnilib Rengine.o jri.o Rcallbacks.o Rinit.o globals.o rjava.o -dynamiclib -framework JavaVM -fopenmp -L/usr/local/lib -L/Users/ssconno/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/lib -lomp -F/Library/Frameworks/R.framework/.. -framework R -lpcre -llzma -lbz2 -lz -licucore -lm -liconv
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’
Warning in install.packages :
installation of package ‘rJava’ had non-zero exit status”

Thanks again for your help!

anonymousNovember 2nd, 2017 at 11:21 pm

Hi there,

just make sure to create file: ~/.R/Makevars and put the content there. E.g. In my case it looks like this:

> cat ~/.R/Makevars
CLANG=/Users/michalo/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0
CC=$(CLANG)/bin/clang
CXX=$(CLANG)/bin/clang++
CXX1X=$(CLANG)/bin/clang++
CXXFLAGS=-I$(CLANG)/include
CXXLDFLAGS=-L$(CLANG)/lib

anonymousNovember 18th, 2017 at 11:11 am

Hi Micha,
Thank you for this post, it has been very helpful.
I can install the rJava library when I run R in the terminal. But this doesn’t work when I run the Rapp. Is it possible to fix that?
Thanks.

anonymousNovember 20th, 2017 at 12:39 pm

It might be that you are using completely different R installation. Try choosing another one. Go to: https://support.rstudio.com/hc/en-us/articles/200486138-Using-Different-Versions-of-R and take a look how to maintain R versions used by R

anonymousDecember 9th, 2017 at 6:29 pm

# Make sure to put following line inside you ~/.profile
what does this mean? I am trying to configure R to use XLConnect. Please help, if you have some time. Thanks

anonymousDecember 9th, 2017 at 7:16 pm

You need to open file inside your HOME directory. This file has name “.profile”.

You can use any editor you like. I suggest TextMate, Sublime Text or VIM.

anonymousJanuary 25th, 2018 at 2:26 am

This is amazing! Thanks for putting this together for the rest of us who couldn’t possibly understand how to do this!

anonymousJanuary 25th, 2018 at 9:40 am

Thanks! I really appreciate your comment!

anonymousJanuary 29th, 2018 at 12:54 am

Hi Michal, this has been super helpful both for working through these problems and learning some more about my Mac/Java/R installation.

The issue I’m having is when attempting to configure R. I’ve followed all the steps above (downloading a new clang, installing gfortran, enabling the JDK for JDI in *Info.plist*, and making changes to the *Makeconf* and *Makevars* files). However, I started using the previous walk-through you did before moving on to the 3.4 update, so perhaps I screwed something up inadvertently?

Here is the error I get when running **sudo R CMD javareconf**:

—–
dir:~ myname$ sudo R CMD javareconf
Java interpreter : /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java
Java version : 1.8.0_144
Java home path : /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre
Java compiler : /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/javac
Java headers gen.: /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/javah
Java archive tool: /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/jar
Non-system Java on macOS

trying to compile and link a JNI program
detected JNI cpp flags : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
/Users/name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/../include/darwin -I/usr/local/include -fPIC -Wall -g -O2 -c conftest.c -o conftest.o
/Users/myname/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/server -ljvm SHLIB_LIBADD = -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
clang-4.0: error: no such file or directory: ‘SHLIB_LIBADD’
clang-4.0: error: no such file or directory: ‘=’
make: *** [conftest.so] Error 1
Unable to compile a JNI program
—-

When I look in *Makeconf*, the corresponding entry is “SHLIB_LIBADD = “, so perhaps this needs to be replaced by something else?

Any help would be very much appreciated, this issue has been plaguing me for weeks!

anonymousJanuary 30th, 2018 at 10:14 pm

This one is strange:

SHLIB_LIBADD = …

I don’t recall facing this parameter while running R java configuration.

I’d simply have this line removed.

anonymousFebruary 1st, 2018 at 3:31 pm

Thanks so much for your help Michal!

I tried deleting the SHLIB_LIBADD line but got another error … ended up fixing the issue by uninstalling and reinstalling R. Now, everything is find until I attempt to install rJava; I’m still getting an error with the JNI “types” (new to Java, not totally sure what this is). Any other suggestions would be appreciated!

—-
usr:~ myname$ R CMD INSTALL /Users/myname/Desktop/rJava_0.9-9.tar.gz
* installing to library ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library’
* installing *source* package ‘rJava’ …
** package ‘rJava’ successfully unpacked and MD5 sums checked
checking for gcc… /Users/myname/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang
checking whether the C compiler works… yes
checking for C compiler default output file name… a.out
checking for suffix of executables…
checking whether we are cross compiling… no
checking for suffix of object files… o
checking whether we are using the GNU C compiler… yes
checking whether /Users/myname/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang accepts -g… yes
checking for /Users/myname/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang option to accept ISO C89… none needed
checking how to run the C preprocessor… /Users/juliaclark/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang -E
checking for grep that handles long lines and -e… /usr/bin/grep
checking for egrep… /usr/bin/grep -E
checking for ANSI C header files… rm: conftest.dSYM: is a directory
rm: conftest.dSYM: is a directory
yes
checking for sys/wait.h that is POSIX.1 compatible… yes
checking for sys/types.h… yes
checking for sys/stat.h… yes
checking for stdlib.h… yes
checking for string.h… yes
checking for memory.h… yes
checking for strings.h… yes
checking for inttypes.h… yes
checking for stdint.h… yes
checking for unistd.h… yes
checking for string.h… (cached) yes
checking sys/time.h usability… yes
checking sys/time.h presence… yes
checking for sys/time.h… yes
checking for unistd.h… (cached) yes
checking for an ANSI C-conforming const… yes
checking whether time.h and sys/time.h may both be included… yes
configure: checking whether /Users/myname/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang supports static inline…
yes
checking whether setjmp.h is POSIX.1 compatible… yes
checking whether sigsetjmp is declared… yes
checking whether siglongjmp is declared… yes
checking Java support in R… present:
interpreter : ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/../bin/java’
archiver : ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/../bin/jar’
compiler : ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/../bin/javac’
header prep.: ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/../bin/javah’
cpp flags : ‘-I/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/../include/darwin’
java libs : ‘-L/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/server -ljvm’
checking whether Java run-time works… yes
checking whether -Xrs is supported… yes
checking whether JNI programs can be compiled… yes
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.
ERROR: configuration failed for package ‘rJava’
* removing ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava’

anonymousFebruary 2nd, 2018 at 10:53 am

I’ve been stuck at

# Make sure to put following line inside you ~/.profile

I can’t seem to find this file anywhere in the home directory, assuming the home directory is this:
/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home

anonymousFebruary 2nd, 2018 at 5:12 pm

In fact, I am referring here to $HOME – that is, your home directory with all your files. And .profile is a file where you can store some info for bash. You can read more about .profile by calling “man bash”.

anonymousFebruary 7th, 2018 at 9:10 am

Thank you so much michal. I’ve managed to make my way to the last few steps. Now I’m stuck at trying to download R and there is an error of “One or more JNI types” as some other users have experienced.

I called ./configure and it returned this log:
“.
.
.
checking Java support in R… present:
interpreter : ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/../bin/java’
archiver : ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/../bin/jar’
compiler : ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/../bin/javac’
header prep.: ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/../bin/javah’
cpp flags : ‘-I/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/../include/darwin’
java libs : ‘-L/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/server -ljvm’
checking whether Java run-time works… Error: Could not find or load main class getsp
no
configure: error: Java interpreter ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/../bin/java’ does not work”

anonymousFebruary 7th, 2018 at 10:22 am

Managed to download rJava successfully but it returned with “warning: ‘JNI_GetCreatedJavaVMs’ is deprecated”

As I called .jinit(), the following error was returned:

JavaVM: Failed to load JVM: /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bundle/Libraries/libserver.dylib
JavaVM FATAL: Failed to load the jvm library.
Error in .jinit() : JNI_GetCreatedJavaVMs returned -1

anonymousFebruary 7th, 2018 at 4:09 pm

It looks like this library was moved to this location:

/Library/Java/JavaVirtualMachines/jdk1.8.0_###.jdk/Contents/Home/jre/lib/server

You can play with symbolic links or you can try to force R to use this location.

anonymousFebruary 8th, 2018 at 7:53 am

I still got the same error after trying everything here: https://github.com/MTFA/CohortEx/wiki/Run-rJava-with-RStudio-under-OSX-10.10,-10.11-(El-Capitan)-or-10.12-(Sierra)

Despite forcing it to load it from that location, it still refuses to when I call .jinit().

anonymousFebruary 8th, 2018 at 8:27 am

I’ve finally managed to get it to work using the solution suggested here: https://oliverdowling.com.au/2014/03/28/java-se-8-on-mac-os-x/

Created a directory and linked one to the other.

#sudo mkdir -p /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bundle/Libraries
#sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/server/libjvm.dylib /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bundle/Libraries/libserver.dylib

Thanks for your help, Michal!

anonymousFebruary 17th, 2018 at 12:06 am

Thank you very very much! It worked perfectly :D

anonymousFebruary 17th, 2018 at 10:28 am

Cool! Have fun with R and Java ;)

anonymousMay 8th, 2018 at 8:19 pm

After trying all that I still get this error reg. C compiler while installing rJava: install.packages(“rJava”,dependencies = TRUE)
Installing package into ‘/Users/vepakom/Library/R/3.4/library’
(as ‘lib’ is unspecified)
trying URL ‘https://mirrors.sorengard.com/cran/src/contrib/rJava_0.9-9.tar.gz’
Content type ‘application/x-gzip’ length 660454 bytes (644 KB)
==================================================
downloaded 644 KB

* installing *source* package ‘rJava’ …
** package ‘rJava’ successfully unpacked and MD5 sums checked
checking for gcc… /Users/user_name/opt/clang+llvm-4.0.1-x86_64-apple-macosx10.9.0/bin/clang
checking whether the C compiler works… no
configure: error: in `/private/var/folders/tp/pb6fwh7516qdtzdpvc4h23dn_tscsv/T/RtmpgP5mMB/R.INSTALL861f2370e0d4/rJava’:
configure: error: C compiler cannot create executables
See `config.log’ for more details
ERROR: configuration failed for package ‘rJava’
* removing ‘/Users/vepakom/Library/R/3.4/library/rJava’
* restoring previous ‘/Users/vepakom/Library/R/3.4/library/rJava’

The downloaded source packages are in
‘/private/var/folders/tp/pb6fwh7516qdtzdpvc4h23dn_tscsv/T/RtmpeAYE9J/downloaded_packages’
Warning message:
In install.packages(“rJava”, dependencies = TRUE) :
installation of package ‘rJava’ had non-zero exit status

anonymousMay 9th, 2018 at 8:07 am

You have to look here:

“See `config.log’ for more details”

I bet, there is some sort of info what has failed during build process.

anonymousMay 19th, 2018 at 6:01 pm

Hi Michal,

As others have mentioned, this is a great tutorial and thank you for sharing.

I’ve been struggling with this a bit and wondering if the above steps will work for R 3.5 or if you are intending to update it accordingly?

I will keep going through the steps and comments but wanted to ask the question in case R 3.5 changes things.

Thanks again!

Will

anonymousMay 19th, 2018 at 10:45 pm

Thanks!

Probably, at some point I will update all the stuff for most recent release of R/RStudio/XCode/clang/Java 9/Java 10, etc.

I don’t have strong need to upgrade to R3.5 already, but once I have, tutorial will be update as well ;)

So, keep your finger on the pulse, and make sure to register to my rss. This way, you will get info as soon as article is published.

AmaoJune 23rd, 2018 at 7:13 am

Hi,

I’ve been having this issue for over two weeks now, i’m a newbee to terminal and so far i’ve tried several times to follow your tutorials and i got stuck at this stage because some of your instructions are for more experience users … can you elaborate more please

> echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
/usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
Make sure to enable your JDK for JNI

> sudo vi /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Info.plist

# make sure to replace
#
# CommandLine
#
# with
#
# CommandLine
# JNI
#

when i type the ” echo $JAVA_HOME”, i don’t get anything back and the Vi command also opens a blank file.

my session info is as follows , will appreciate any help. for fear of not messing my system up, i’ve already restore twice
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.5

IrinaJuly 18th, 2018 at 1:52 pm

Thanks! Works!

Leave a comment

Your comment