R, loading rJava error

MrMax picture MrMax · May 24, 2017 · Viewed 13.2k times · Source

I'm running R (version 3.4.0) on macOS sierra (10.12.5) and I'm trying to use the xlsx library which requires the use of the rJava library.

When I try to load rJava using library(rJava) in the R console (no difference if in RStudio or in the terminal), I get the following error message which I'm having trouble making sense of:

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

Googling the problem I found a number of posts on somewhat similar (but each time slightly different problems), this SO question being the most promising among them.

Since the steps of the proposed require adaptation to my case and I'm kind of a novice, I wasn't able to implement them. What can I do?

Additional information:

The shell command java -version returns:

java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

And the shell command R CMD javareconf returns:

Java interpreter : /usr/bin/java
Java version     : 1.8.0_131
Java home path   : /Library/Java/JavaVirtualMachines/jdk1.8.0_131.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_131.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/../include/darwin  -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/jdk1.8.0_131.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_131.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.

Any help will be greatly appreciated!

Answer

MrMax picture MrMax · May 24, 2017

It works!

After installing and reinstalling different java versions and reinstalling rJava with the R command install.packages("rJava") I got to the point where loading the library in the R console (shell window) didn't throw an error message anymore.

After that I was able to use the answer in this post to get it to work in RStudio by running this shell command:

sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib

Not too complicated after all! Thank you for everyones patience.