rJava failed to load, can't. Error says unable to load rJava.so despite having JDK installed and running javareconf

This might be more of a general R environment problem, but I came across it while trying to load XLConnect after a clean reinstall, so I figured I'd try here.

I was previously using XLConnect as part of a notebook successfully. Yesterday, however, I decided it was time to do a clean install of my mac for reasons that I won't go into. So I have a clean install of macOS, I installed R and Rstudio, then started to install the packages I use. Everything worked apart from XLConnect, because it has that pesky java dependancy, so I installed JRE first and the JDK. Here's what I currently have

macOS 10.14.6
R 3.6.1
rStudio 1.2.5001
java version "13" 2019-09-17
Java(TM) SE Runtime Environment (build 13+33)
Java HotSpot(TM) 64-Bit Server VM (build 13+33, mixed mode, sharing)

When it still wouldn't work, I ran:

sudo R CMD javareconf

Unfortunately, that also didn't work.

I checked the traceback of the XLConnect library error to confirm that the problem is rJava. I tried reinstalling rJava, but that also didn't help.

When I try to load the rJava library, this is the error

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.6/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.6/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.6/Resources/library/rJava/libs/rJava.so
Reason: image not found

This is confusing, as I thought the javareconf command was supposed to take care of the necessary environment variables, so I'm not sure why it won't work. I'm a bit out of my depth here. If it helps, this is the output from javareconf

Java interpreter : /usr/bin/java
Java version : 13
Java home path : /Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar

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/jdk-13.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home/include/darwin -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -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/jdk-13.jdk/Contents/Home/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation

JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home
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.

Edit: one more thing. I checked that
/Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so
does exist, so this image not found error that r is throwing doesn't help much.

I'm not a Mac user but at least on Linux (headless system) you need to restart the system after javareconf for changes to take effect, give it a try.

Thanks andresrcs. I'd have felt a right idiot if the solution was to turn it off and back on again, but unfortunately, rebooting didn't resolve the issue.

This may be a bit of a long shot, and I don't play with rJava near as much as I used to, but if you look at the output of Sys.getenv(), is JAVA_HOME set in your R session? I know that in some cases, manually setting JAVA_HOME in .bashrc, .Renviron, Sys.setenv(), or otherwise helps R find the appropriate java before you load any packages like rJava...

I'm curious what the output of that would be!

Do you need to use XLconnect? Because tidyxl is a lot better and easier to use and doesn't require java.

I have exactly the same problem.
macOS 10.14.6
R 3.6.1
rStudio 1.2.5001

My JDK version is bit lower jdk-11.0.4. I've tried setting JAVA_HOME,DYLD_LIBRARY_PATH, DYLD_FALLBACK_LIBRARY_PATH in .Rprofile. I've tried dyn.load(paste0(system('/usr/libexec/java_home -v 11', TRUE), '/lib/server/libjvm.dylib') but nothing helps. R from command line works perfectly and is configured for java with R CMD javareconf. rJava in RStudio just cant find the jvm.

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.6/Resources/library/rJava/libs/rJava.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.6/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.6/Resources/library/rJava/libs/rJava.so
Reason: image not found

Only way I can get it work is an ugly trick:
Create a folder
sudo mkdir /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/
cd to it and create a symbolic link to existing libjvm.dylib:
sudo ln -s /Library/Java/JavaVirtualMachines/jdk-11.0.4.jdk/Contents/Home/lib/server/libjvm.dylib libjvm.dylib

I suppose this is not the way it should be configured.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.