EDIT: I don't know if anyone is still struggling with this, but with rJava 0.9-9, the 'partial fix' below no longer works. What does work, and completely, is the final solution offered here: https://github.com/s-u/rJava/issues/86
Copying from there, many thanks to Gregory R. Warnes:
uninstall existing rJava versions by running the following in the Terminal:
Rscript -e 'remove.packages("rJava")'
sudo Rscript -e 'remove.packages("rJava")'
add the following to /Users/<userid>/.bashrc
:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)/jre'
(e.g., type > vim /Users/<userid>/.bashrc
in the Terminal, then 'i', add the line above, then ':wq' to save and quit)
close and re-start all Terminal, R and RStudio windows
type the following in the Terminal window:
sudo ln -sf $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
in a new R session, re-install rJava from source:
install.packages("rJava", repos="http://rforge.net", type="source")
OLD 'PARTIAL FIX' BELOW:
Okay. I have been working on this problem all morning, and I have a partial fix.
I tried the solution suggested by Guilherme Kenji Chihaya above, but even after sudo R CMD javareconf -n
and install.packages('rJava', type='source')
, R insists on using Java 1.6 (and is happy to do so).
HOWEVER, R studio throws an error after re-installing rJava:
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.2/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so
Reason: image not found
Error: package or namespace load failed for ‘rJava’
Googling this lead me to this post: http://andrewgoldstone.com/blog/2015/02/03/rjava/, with a working solution. Set the following in the Terminal:
alias r="DYLD_FALLBACK_LIBRARY_PATH=/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/server/: open -a r"
And start R from the Terminal. Then, magically, in R:
> library(rJava)
> .jinit()
> .jcall("java/lang/System", "S", "getProperty", "java.runtime.version")
[1] "1.8.0_11-b12"
However, this only works when starting R from the Terminal. I haven't been able to get R to automatically recognise the right "DYLD_FALLBACK_LIBRARY_PATH" in any way.