Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R, loading rJava error

Tags:

java

r

rjava

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!

like image 515
MrMax Avatar asked May 24 '17 12:05

MrMax


People also ask

What is rJava package in R?

rJava provides a low-level bridge between R and Java (via JNI). It allows to create objects, call methods and access fields of Java objects from R. rJava release versions can be obtained from CRAN - usually install. packages("rJava") in R will do the trick.

What is rJava?

rJava is a JNI (Java Native Interface)-based, R-to-Java interface with which Java objects may be created and Java methods called and Java fields accessed from R. In this tutorial, we shall get started with using the rJava R package to use Java from R. This tutorial has the following sections: Setting the Environment.

What is rJava package?

The rJava package gives access to low-level R functions to the Java interface, but it is not provided with TIBCO Enterprise Runtime for R. These instructions help you prepare your computer to use rJava.


1 Answers

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.

like image 170
MrMax Avatar answered Sep 21 '22 15:09

MrMax