Errors loading rJava - ".onLoad failed in loadNamespace() for 'rJava', details..."

I have trouble running "library(xlsx)". Every time I run it, I get an error( See the attachment). I already downloaded package readxl and xlsx and java, but it still cannot work. How to set the Java environment variable in R?

BTW, my computer system is Mac OS. Could you help me with this, please?

Does this work for you in R, but not in RStudio? (see FAQ below for disambiguation)

If you're on a Mac, this guide can be helpful for getting rJava running right:

Thank you! Could you tell me how to find the path of Java8 on my computer? I'm stuck here

Hi there,

I have been receiving error messages about a installed package Rweka.

library(Rweka)
Error: package or namespace load failed for ‘RWeka’:
.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-9.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

The background of the OS is macOS High Sierra, RStudio Version 1.1.456, R version 3.5.1. With the previous error I have installed the rJava package and the JRE version 10.0.2.
Additionally, I have also ran this code

sudo R CMD javareconf

as suggested here.

I would greatly appreciate for any suggestions that will resolve this issue.
Thank you,
Bradley

1 Like

Does this work for you in R, but not in RStudio? (see FAQ below for disambiguation)

If you're on a Mac, this guide can be helpful for getting rJava running right:

1 Like

Hi @dataandme,

I have tried to install and run rJava in R. But I am seeing the same errors.

install.packages("rJava")
--- Please select a CRAN mirror for use in this session ---
trying URL 'https://mirrors.sorengard.com/cran/bin/macosx/el-capitan/contrib/3.5/rJava_0.9-10.tgz'
Content type 'application/octet-stream' length 735656 bytes (718 KB)
==================================================
downloaded 718 KB
The downloaded binary packages are in
/var/folders/k7/5mqy2vtj3d76plpc758dthvm0000gn/T//RtmpOdW3Ys/downloaded_packages

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 '/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-9.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

Additionally the Installing rJava on macOS does not work.

Thanks

Well, you're definitely not the only one who struggles with the rJava installation (I wish I had more careful notes form when I finally got my installation to work)—it's a commonly problematic install.

This thread seems to be struggling with the same issue:

We've hit the limit of my rJava skills. @xujingsi, if you don't mind (and you as well @bradvision) could we combine this threads, and rename with something about rJava (since that seems to be the root problem). That way, hopefully we'll be able to get more-expert eyes on it.

Do any of the suggestions here work for you?

There are a few other possibilities for getting rJava installed correctly in this StackOverflow thread, as well:

More extensive instructions available here:

Thank you, Mara. I found the path, but I encountered another problem. Something went wrong when I set Java 8 gloablly.

I'm not sure what's going on there. Hopefully someone else will know. In the future, fi you could paste the text of the error messages, it's easier to read than screenshots, and it will make searching for them easier as well!

Hi everyone,

I have been able to access Zhiya Zho's rJava installation guide. I am very thankful of the guide that he put together. But I am still coming up in errors in terminal when I am running part 5 R CMD javareconf in terminal. I am unable to bypass this in order to install rJava. As per Zhiya's suggestion in the comments under the installation guide I have installed the Xcode Command Line Tools. Yet, I am running into errors at R CMD javareconf. I would appreciate if anyone else could provide some other solution.

MacBook-Pro:.R username $ R CMD javareconf
Java interpreter : /Users/username/.jenv/shims/java
Java version : 1.8.0_181
Java home path : /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre
Java compiler : /Users/username/.jenv/shims/javac
Java headers gen.: /Users/username/.jenv/shims/javah
Java archive tool: /Users/username/.jenv/shims/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
/usr/local/Cellar/gcc/7.2.0/bin/gcc-7 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/../include/darwin -I/usr/local/include -fPIC -Wall -g -O2 -c conftest.c -o conftest.o
/bin/sh: /usr/local/Cellar/gcc/7.2.0/bin/gcc-7: No such file or directory
make: *** [conftest.o] Error 127
Unable to compile a JNI program

JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre
Java library path:
JNI cpp flags :
JNI linker flags :
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.

MacBook-Pro:.R username$

Thank you, Mara. It's a good suggestion. I didn't notice before.
I will paste the text here and hope someone else will help me.

I encountered a problem when I am running the last code of part 3 jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home oracle64-1.8.0_162 added

I already found the path on my computer,

wireless-10-105-238-245:~ jingsixu$ ls -l `which java`
lrwxr-xr-x  1 root  wheel  74 Mar 28 05:53 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

However, this path didn't work.

wireless-10-105-238-245:~ jingsixu$ jenv add /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java is not a valid path to java installation
wireless-10-105-238-245:~ jingsixu$ oracle64-1.8.0_162 added
-bash: oracle64-1.8.0_162: command not found
wireless-10-105-238-245:~ jingsixu$ jenv add /Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java is not a valid path to java installation
wireless-10-105-238-245:~ jingsixu$ oracle64-1.8.0_162 added
1 Like

Hi, Bradvison. I finally figured out what's wrong on this step.
you may need to check out do you have this folder? "/usr/local/Cellar/gcc/7.2.0/bin/gcc-7"
The way to check out: Finder-->Go-->Go to folder

For me, it is " /usr/local/Cellar/gcc/8.2.0/bin/gcc-8"

And you might need to edit on the step4 and check out if you save it successfully.

Hi everyone,
I successfully followed all the things written in Zhiya Zuo's rJava installation guide
but I encountered another error when I tested

> library(xlsx)
Error: package or namespace load failed for ‘xlsx’ in get(Info[i, 1], envir = env):
 lazy-load database '/Library/Frameworks/R.framework/Versions/3.5/Resources
/library/rJava/R/rJava.rdb' is corrupt
In addition: Warning message:
In get(Info[i, 1], envir = env) : internal error -3 in R_decompress1

I don't know what this means.
Hope someone can help me. Thanks

Jingsi

I reopened the R studio and rerun the code. no error happen again. So Happy!

Hi, xujingsi

Pardon me for not responding within the day. So I too followed the Zhiya Zuo's installation guide and it appears that I have successfully installed rJava. Yet, I am unable for the RWeka package to work. Hence, I am unsure whether the rJava installation was successful. I will looking into your suggestions and get back to you.

Thank you,
Bradley

Hi xujingsi,

I was having the same problem with "library(rJava)" in RStudio on Windows 10.
I updated Java to the latest version, just to make sure it was 64-bit, and reinstalled RStudio and library(rJava) worked.

Ralph

Everything worked fine with R 3.6.1 + RStudio 1.2.1335 on MacOS Mojave 10.14.6.
This "image not found" problem occurred since upgrading RStudio to 1.2.1578, and I've tried the following:

  • library(rJava) in RStudio - FAILED
  • library(rJava) in terminal - OK
  • Resetting RStudio settings and reinstall RStudio - NO HELP
  • sudo R CMD javareconf; R CMD javareconf - NO HELP
  • In RStudio run dyn.load('/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/jre/lib/server/libjvm.dylib'), then library(rJava) - FAILED
  • reinstall rJava package - NO HELP
  • Finally,
install_name_tool -change /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib \
/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/jre/lib/server/libjvm.dylib \
/Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so

then library(rJava) in RStudio - SUCCESS :smile::smile:

Reference: rJava #151

1 Like

Thank you Ming, your solution was the one update that worked for me after trying many other cases that you noted as well. My setup is MacOS Mojave 10.14.6, R version 3.6.1, RStudio Version 1.2.5001. Each time I attempted to load rJava I noticed what appeared to be that hardcoded reference to jdk-11.0.1.jdk in the path. In my case I've installed the latest Java SE Development Kit (version 13 as of writing this comment) so the updated path I needed to write was jdk-13.jdk.

Also noting that I recognized one other difference between my update and your update which is that the path to the libjvm.dylib file for Version 13 does not include the jre subfolder below Home (i.e. the path Contents/Home/jre/lib/server/libjvm.dylib for Version 13 is Contents/Home/lib/server/libjvm.dylib) as opposed to when you've installed Version 8 and wish to override the path to reference that version like in your example.

In summary this is the script I ran which successfully resolved my issue:

install_name_tool -change /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/jre/lib/server/libjvm.dylib \
/Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home/lib/server/libjvm.dylib \
/Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so
1 Like