I have asked a similar question before, but didn't get any answers that I could actually implement. I've also read the answers here and here, but couldn't implement any of them (partially because no answer actually retraces all of its steps.
Problem
I'm getting an error when trying to load rJava on R 3.5.2 and MacOS Mojave 10.14.3:
> library(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.5/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found
Attempted Solutions
I first tried this solution, which didn't work. Then, based on this answer, I tried the following:
Entered the following on Terminal:
$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
Then tried the following on R:
> options("java.home"="/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home")
>
> Sys.setenv(LD_LIBRARY_PATH='$JAVA_HOME/server')
>
> dyn.load('/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib/server/libjvm.dylib')
>
> library(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.5/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found
I've also tried:
> options("java.home"="/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/bin")
> Sys.setenv(LD_LIBRARY_PATH='$JAVA_HOME/server')
> dyn.load('/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib/server/libjvm.dylib')
> library(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.5/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/rJava.so
Reason: image not found
In the past, I've downgraded succesfully to R 3.1, but I'd rather not have to do that every time I want to use rJava
This is the setup that works for me
macOS Mojave - 10.14.3
XQuartz - version 2.7.11 - https://www.xquartz.org
R - version 5.3.2
Java - 11.0.1
rJava - installed from CRAN
Update
In your case, it looks like JDK used during installation/compilation of rJava is missing. You can tell that from the message:
Make sure this file exists:
Probably, it's missing. You have two options here: install Java 11.0.1, reconfigure R.
You can list all the available JVM installations using
/usr/libexec/java_home -V
Reproducing initial issue, and fixing it
Let's pretend we have removed JDK 11.0.1
Let's try to load rJava
Let's fix it
It should work now
I had a similar problem. Its usually best if you start from scratch if you have tried all these solutions and nothing worked. You wouldnt want one of these "fixes" to cause you more issues because most of them require you to change environment variables.
Here is how I fixed it.
Install the latest JAVA JDK https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Run
sudo R CMD javareconf
in the terminal. Dont change any of the environment variables on your own.Hope it helps.