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
If you are looking for more JNI samples, take a look here: http://jnicookbook.owsiak.org
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!
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).
Take a look here: http://www.owsiak.org/?p=3718
BTW, you have a typo
JAVA=${JAVA_HOME/bin/java \
JAVA=${JAVA_HOME}/bin/java \
Thanks for spotting it!
Fixed
I got the same error as Luis after following the steps as well
Take a look here: http://www.owsiak.org/?p=3718
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!
Take a look here: http://www.owsiak.org/?p=3718
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!
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 :(
Take a look here: http://www.owsiak.org/?p=3718 – this one should fix the issue
“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
You might need to use “sudo”
sudo ln -s Home RHome
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.
“`
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”.
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
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.
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?
cd /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents
ln -s Home RHome
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?
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.
Michal and Luis, when starting to run Luis’ solution, I got the following message:
Last login: Sat Jan 6 18:23:21 on ttys000
Gilvans-MBP:~ grguedes$ mkdir ~/opt
Gilvans-MBP:~ grguedes$ cd ~/opt
Gilvans-MBP:opt grguedes$ 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
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 –:–:– 0:00:22 –:–:– 0
100 447 100 447 0 0 7 0 0:01:03 0:01:02 0:00:01 130
Gilvans-MBP:opt grguedes$
Gilvans-MBP:opt grguedes$ tar xf clang+llvm-4.0.1-x86_64-apple-darwin.tar.xz
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.
Gilvans-MBP:opt grguedes$
Where is the problem?
Thank you!
Try this one instead:
curl -O http://releases.llvm.org/4.0.1/clang+llvm-4.0.1-x86_64-apple-darwin.tar.xz
From the size of your download it looks like there were some problems while accessing file. You can check it by calling:
cat clang+llvm-4.0.1-x86_64-apple-darwin.tar.xz
Hello
when i try to start rjava i get the following error
Fehler: package or namespace load failed for ‘rJava’:
.onLoad in loadNamespace() für ‘rJava’ fehlgeschlagen, Details:
Aufruf: dyn.load(file, DLLpath = DLLpath, …)
Fehler: kann shared object ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so’ nicht laden:
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
I tried to step through the workaround but i also ended up with this error
manuelkühnle-pc:~ MOK$ sudo R CMD javareconf
Password:
Java interpreter : /usr/bin/java
Java version : 1.6.0_65
Java home path : /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
Non-system Java on macOS
trying to compile and link a JNI program
detected JNI cpp flags :
detected JNI linker flags : -L/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries -ljvm
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c conftest.c -o conftest.o
conftest.c:1:10: fatal error: ‘jni.h’ file not found
#include
^
1 error generated.
make: *** [conftest.o] Error 1
Unable to compile a JNI program
JAVA_HOME : /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Java library path:
JNI cpp flags :
JNI linker flags :
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.
Can anybody help please
It looks like your R configuration mixes two Java installations. Apple’s one and Oracle’s one.
Have you tried to do this trick?
cd /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents
ln -s Home RHome
Have followed all steps above bu I get some errors both when I install and try to load rJava. No clue how to fix or even what is wrong..
library(rJava)
Error : .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.3/Resources/library/rJava/libs/rJava.so’:
dlopen(/Library/Frameworks/R.framework/Versions/3.3/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rJava/libs/rJava.so
Reason: image not found
Error: package or namespace load failed for ‘rJava’
during installation from source I get these warnings:
>clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I. -I/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/RHome/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/RHome/include/darwin -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -fPIC -Wall -mtune=core2 -g -O2 -c Rglue.c -o Rglue.o
Rglue.c:847:13: warning: unused function ‘new_jrectRef’ [-Wunused-function]
static SEXP new_jrectRef(JNIEnv *env, jobject a, const char *sig, SEXP dim ) {
^
1 warning generated.
>/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/RHome/bin/javac -target 1.6 -source 1.6 -d . ../Mutex.java ../RBool.java ../RConsoleOutputStream.java ../REXP.java ../RFactor.java ../RList.java ../RMainLoopCallbacks.java ../RVector.java ../Rengine.java ../package-info.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 warning
>/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/RHome/bin/javac -target 1.6 -source 1.6 -d . ../Mutex.java ../RBool.java ../RConsoleOutputStream.java ../REXP.java ../RFactor.java ../RList.java ../RMainLoopCallbacks.java ../RVector.java ../Rengine.java ../package-info.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 warning
>/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/RHome/bin/javac -target 1.6 -source 1.6 -classpath src/JRI.jar -d examples examples/rtest.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6
Note: examples/rtest.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 warning
>/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/RHome/bin/javac -target 1.6 -source 1.6 -classpath src/JRI.jar -d examples examples/rtest2.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6
Note: examples/rtest2.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 warning
Here is my javareconf output:
sudo R CMD javareconf
Java interpreter : /usr/bin/java
Java version : 1.8.0_161
Java home path : /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/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
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -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 -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -fPIC -Wall -mtune=core2 -g -O2 -c conftest.c -o conftest.o
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_161.jdk/Contents/Home/jre/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.
Strange, it looks like you have mixed Linux/macOS env. Strange, that your system is looking for so files (they are common for Linux).
Yes, very strange and no clue how I ended up in this place…
I did however manage to get it working (I think) via a total random walk through the interwebs.
It ended with me noticing that I was able to load rJava in a session launched in terminal but not through the r.app-interface. In a standard R-session however I could only ger rJava to load after having manually loaded the libjvm.dylib that is mentioned in the load-error in R (@rpath/libjvm.dylib).
After manual load the code below spit out both the installed java version info as well as the allocated memory.
dyn.load(‘/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/server/libjvm.dylib’)
options( java.parameters = “-Xmx4g” )
library(rJava)
.jinit()
.jcall(“java/lang/System”, “S”, “getProperty”, “java.runtime.version”)
library( “RBioFormats” )
checkJavaMemory()
Thanks for taking the time to write this up. Incredibly helpful
Thanks !!
Worked for me on OS X 10.13.4 with fresh jdk-10.0.1.jdk install and rJava_0.9-9
jdk-10.0.1 is my only JVM (singular output of /usr/libexec/java_home -V)
I did not have to edit /usr/libexec/java_home`/../Info.plist
Thanks!
Cool!