Reconfigure rJava to use the appropriate JVM

In the last few days I have been moving posts from old blogs to here and in the process of updating some of the articles I have come across some interesting issues.

Note: This fix applies for MacOS only but steps may be similar for others OS.

This is one of those where at time of writing the article I had an specific JVM/rJava installed in my Macbook air.

This is the error I got:

> 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 '/Users/anegron/Dropbox/rpackages/rJava/libs/rJava.so':
  dlopen(/Users/anegron/Dropbox/rpackages/rJava/libs/rJava.so, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
  Referenced from: /Users/anegron/Dropbox/rpackages/rJava/libs/rJava.so
  Reason: image not found

From the above error you can tell rJava is looking for jdk-11.0.1 which I guess is the latest version. Quick fix is just to update the JVM and problem solves but since I have other tools relying on my current JVM version I did not want to walk down that path.

Reading this github issue provided me with a cunning solution.

The first step is to figure out the JVM version installed in my local machine

R CMD javareconf
## Java interpreter : /usr/bin/java
## Java version     : 10.0.2
## Java home path   : /Library/Java/JavaVirtualMachines/jdk-10.0.2.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-10.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk-10.0.2.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-10.0.2.jdk/Contents/Home/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
## ld: warning: text-based stub file /System/Library/Frameworks//CoreFoundation.framework/CoreFoundation.tbd and library file /System/Library/Frameworks//CoreFoundation.framework/CoreFoundation are out of sync. Falling back to library file for linking.
## 
## 
## JAVA_HOME        : /Library/Java/JavaVirtualMachines/jdk-10.0.2.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.

and then I had to change the path accordingly via install_name_tool -change as follow:

install_name_tool -change \
  /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib \
  /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home/lib/server/libjvm.dylib \
  /Library/Frameworks/R.framework/Resources/library/rJava/libs/rJava.so

Problem solved!

Comments

comments powered by Disqus