3

Like a bunch of people, I am having trouble getting rJava to load in RStudio -- the same problem also reproduces itself when using the R GUI directly.

Here's the error:

> require(rJava)
Loading required package: 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.4/Resources/library/rJava/libs/rJava.so':
  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

I have tried a number of things, including reconfiguring from the command line:

sudo R CMD javareconf -n

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

When I check options("java.home") it was set to NULL, so I set it to the following:

> options("java.home"="/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre")

I also reinstalled Java SE Development Kit 8 and then resintalled rJava.

Regardless, I continue to get the same error.

Here is my session info:

> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X El Capitan 10.11.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.4/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     

loaded via a namespace (and not attached):
 [1] colorspace_1.3-2 scales_0.5.0     compiler_3.4.1   lazyeval_0.2.0  
 [5] plyr_1.8.4       tools_3.4.1      gtable_0.2.0     tibble_1.3.4    
 [9] Rcpp_0.12.12     ggplot2_2.2.1    grid_3.4.1       rlang_0.1.2     
[13] munsell_0.4.3   

Any thoughts would be appreciated.

4 Answers 4

5

I had a similar issue. Quickest solution was to manually load the dylib.

dyn.load('/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/server/libjvm.dylib')
require(rJava)

If the above solution works, a long term solution exists by adding this command to the terminal.

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

Found this solution here. You will no longer need to manually load the dylib each time.

Sign up to request clarification or add additional context in comments.

1 Comment

on my system, with a more recent version of java, the path is different, so the command is dyn.load('/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/lib/server/libjvm.dylib')
1

When re-installing java on a Mac OX system, you need to close all your r sessions and then to use the system command line R CMD javareconf

1 Comment

Thanks, I tried this and it does not fix the problem
0

This is something that took me quite lot of time to solve in mac OS.

You can try these:

R 3.4, rJava, macOS and even more mess ;) and this one R, Java, rJava and macOS adventures

You can find quite detailed instructions there. However, note that some people face system specific issues following these instructions :(

And it's hardly possible to find solution that solves all the issues people find.

Comments

0

For me this instruction did it:

https://cimentadaj.github.io/blog/2018-05-25-installing-rjava-on-windows-10/installing-rjava-on-windows-10/

if you have installed Java before try to deinstall and reinstall it like:

Windows 10 - Uninstall Programs

  1. Click Start.
  2. Select Settings.
  3. Select System.
  4. Select Apps & features.
  5. Select the program to uninstall and then click its Uninstall button.
  6. Respond to the prompts to complete the uninstall.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.