Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load rJava into RStudio?

Tags:

r

rstudio

rjava

Although I'm running the risk of duplicating questions, I have yet to find a solution to my configuration.

I'm trying to load rJava into RStudio.

I am using the latest Mac OS x, with a build of Rstudio that's only a couple of months old, and I've just installed JDK x64 12.0.1 The version of rJava is 0.9-11

On executing:

library(rJava)

I am confronted with the usual:

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 tried setting my Java home path to:

Sys.setenv(JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home')

But that doesn't make any difference.

I've clearly missed the rJava instructions somewhere which lays out how to install the package on all computer setups. I'd appreciate some assistance.

EDIT #1

Following the link provided in the comments below, I managed to get the latest version of Java JDK running through rJava in RStudio. The document "R, Java, rJava and macOS adventures" isn't complete and doesn't work off the cuff. You'll probably end up hacking about to get it working.

I needed rJava to run venneuler, unfortunately, this was not work using the latest Oracle JDK version 12:

WARNING: Initial Java 12 release has broken JNI support and does NOT work. Use stable Java 11 (or watch for 12u if available). ERROR: Java exception occurred during rJava bootstrap - see stderr for Java stack trace.

I'm going to try (wish me luck) to get two versions of Java on my machine and have RStudio recognise the right one.

Edit 2

Pure luck I think I have Java 11 working: enter image description here

like image 746
Anthony Nash Avatar asked Oct 24 '25 08:10

Anthony Nash


1 Answers

I had the this issue as well on my mac (OSX High Sierra v10.13.6) and could not solve it by following previous suggestions (reinstall JDK, R CMD javareconf or setting JAVA_HOME). The same error message persisted:

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

After looking closely at the error message, I found that I had JDK12, but R kept looking for libjvm.dylib under the JDK11 folder which does not exist in my system. The path to JDK11 appeared in Sys.get('DYLD_FALLBACK_LIBRARY_PATH'), but overwriting this variable did not help. Neither did dyn.load('PATH_TO_YOUR_JDK').

And then the second to last line of the error message caught my eye: the path to libjvm.dylib was referenced from rJava.so. Why not take a look at this file? At the end, I opened the rJava.so, found the JDK11 path and replaced it with JDK12 path, and rJava loaded perfectly. Hope this helps other people with the same issue.

R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tabulizer_0.2.2 rJava_0.9-11   

loaded via a namespace (and not attached):
 [1] tabulizerjars_1.0.1 compiler_3.5.2      assertthat_0.2.1    cli_1.1.0           tools_3.5.2         withr_2.1.2        
 [7] rstudioapi_0.10     yaml_2.2.0          crayon_1.3.4        png_0.1-7           sessioninfo_1.1.1  
like image 50
Lin shu-hong Avatar answered Oct 26 '25 22:10

Lin shu-hong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!