Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rJava fails to load #2254

Closed
kevinushey opened this issue Feb 22, 2018 · 52 comments
Closed

rJava fails to load #2254

kevinushey opened this issue Feb 22, 2018 · 52 comments
Labels
Milestone

Comments

@kevinushey
Copy link
Contributor

System details

RStudio Edition : Desktop
RStudio Version : v1.2.331
OS Version      : macOS 10.13.3
R Version       : 3.4.3

Steps to reproduce the problem

library(rJava)

Describe the problem in detail

> 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/kevin/Library/R/3.4/library/rJava/libs/rJava.so':
  dlopen(/Users/kevin/Library/R/3.4/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
  Referenced from: /Users/kevin/Library/R/3.4/library/rJava/libs/rJava.so
  Reason: image not found

Describe the behavior you expected

We should be able to load rJava.

Notes

It looks like, in v1.2, we're setting the wrong environment variable:

> Sys.getenv("DYLD_FALLBACK_LIBRARY_PATH")
[1] "/Library/Frameworks/R.framework/Resources/lib:/Users/kevin/lib:/usr/local/lib:/usr/lib::"
> Sys.getenv("DYLD_LIBRARY_PATH")
[1] "/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/server"
@kevinushey kevinushey added the bug label Feb 22, 2018
@kevinushey kevinushey added this to the v1.2 milestone Feb 22, 2018
@jmcphers
Copy link
Member

I think this is because the r-ldpath script is not being read. In 1.1 we had a special macOS location for the script:

FilePath rLdScriptPath = options.scriptsPath().complete("session/r-ldpath");
if (!rLdScriptPath.exists())
{
FilePath exePath;
Error error = core::system::executablePath(NULL, &exePath);
if (error)
LOG_ERROR(error);
rLdScriptPath = exePath.parent().complete("r-ldpath");
}

But in 1.2 we just look in supporting files:

// determine rLdPaths script location
FilePath supportingFilePath = options.supportingFilePath();
FilePath rLdScriptPath = supportingFilePath.complete("bin/r-ldpath");
if (!rLdScriptPath.exists())
rLdScriptPath = supportingFilePath.complete("session/r-ldpath");

@ronblum
Copy link
Contributor

ronblum commented Mar 12, 2018

Note that this problem occurs in R, not just RStudio.

The problem still occurs in Mac Desktop 1.2.405. I tried with both Java 1.8.0_161 and 9.0.4. In both cases, they failed with messages similar to, but not exactly, as above (probably because we have R installed in different places:

> 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.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

DYLD_LIBRARY_PATH points to the current installations, while DYLD_FALLBACK_LIBRARY_PATH points to a non-existent .../jdk1.8.0_121.jdk/... directory.

On the instance with Java 9.0.4:

> Sys.getenv("DYLD_FALLBACK_LIBRARY_PATH")
[1] "/Library/Frameworks/R.framework/Resources/lib:/Users/ronblum/lib:/usr/local/lib:/usr/lib:::/lib:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/server"
> Sys.getenv("DYLD_LIBRARY_PATH")
[1] "/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/lib/server"

@ronblum ronblum reopened this Mar 12, 2018
@kevinushey
Copy link
Contributor Author

@ronblum: does running

sudo R CMD javareconf

help in this case? (That typically needs to be run after an update of the system Java installation)

@ronblum
Copy link
Contributor

ronblum commented Mar 12, 2018

Yes—thank you!

@sanjmeh
Copy link

sanjmeh commented Jul 7, 2018

I don't think this rJava problem on MAC OS is anywhere but solved. I have 3 macs all updated to OS HighSierra (10.13.5). On every Mac this problem is there and when I move from one mac to another I usually have to repeat all the steps but this time it is being very sticky.

There is a huge chunk of error messages and I am not sure which of the steps are the valid sequence. I tried downloading, restarting R session, reconfiguring by

sudo R CMD javareconf

But no success.

When I run

> library(rJava)

Unable to find any JVMs matching version "(null)".
No Java runtime present, try --request to install.
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
In addition: Warning messages:
1: In system("/usr/libexec/java_home", intern = TRUE) :
running command '/usr/libexec/java_home' had status 1
2: In system("/usr/libexec/java_home", intern = TRUE) :
running command '/usr/libexec/java_home' had status 1
3: In system("/usr/libexec/java_home", intern = TRUE) :
running command '/usr/libexec/java_home' had status 1

When I restart R session:

Restarting R session...

Loading required package: rJava
Unable to find any JVMs matching version "(null)".
No Java runtime present, try --request to install.
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
Error in .requirePackage(package) :
unable to find required package ‘rJava’
In addition: Warning message:
In system("/usr/libexec/java_home", intern = TRUE) :
running command '/usr/libexec/java_home' had status 1
Loading required package: rJava
Unable to find any JVMs matching version "(null)".
No Java runtime present, try --request to install.
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
Error in .requirePackage(package) :
unable to find required package ‘rJava’
In addition: Warning message:
In system("/usr/libexec/java_home", intern = TRUE) :
running command '/usr/libexec/java_home' had status 1
Loading required package: rJava
Unable to find any JVMs matching version "(null)".
No Java runtime present, try --request to install.
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
Loading required package: rJava
Unable to find any JVMs matching version "(null)".
No Java runtime present, try --request to install.
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

I also checked there is nothing in the subdirectory:

ls -l /Library/Java/JavaVirtualMachines

@josempi
Copy link

josempi commented Jul 10, 2018

All, I was able to repair the issue. I have High Sierra 10.13.5, and after updating to the latest Java update and running the "sudo R CMD javareconf" at the terminal prompt it corrected the issue in RStudio.

@kevinushey
Copy link
Contributor Author

@sanjmeh: have you installed a Java Development Kit? You need one if you want to use rJava. See http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html. I'd recommend using JDK 8 for now; I'm not sure if rJava supports the newer JDKs yet.

@KevorkSulahian
Copy link

I also have a similar issue when installing the "rJava" library it gives me the error

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 '/usr/local/lib/R/site-library/rJava/libs/rJava.so':
  libjvm.so: cannot open shared object file: No such file or directory
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/usr/local/lib/R/site-library/rJava’

any help would be appreciated

@finestjava
Copy link

why was issue closed ?

@kevinushey
Copy link
Contributor Author

Because, to the best of our knowledge, the RStudio-side issues are fixed. If you're still having trouble, please open a new issue or consider posting at https://community.rstudio.com.

@finestjava
Copy link

why was issue closed ?

@finestjava
Copy link

Downloading GitHub repo CommerceDataService/eu.us.opendata@master
from URL https://api.github.com/repos/CommerceDataService/eu.us.opendata/zipball/master
Installing eu.us.opendata
'/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file --no-environ --no-save --no-restore --quiet CMD INSTALL
'/private/var/folders/t5/z84y08513157fhxj3cm3kw1h0000gn/T/RtmpbynetV/devtools77cc52eb6fec/CommerceDataService-eu.us.opendata-cc9f934'
--library='/Library/Frameworks/R.framework/Versions/3.5/Resources/library' --install-tests

  • installing source package ‘eu.us.opendata’ ...
    Warning in .write_description(db, file.path(outDir, "DESCRIPTION")) :
    Unknown encoding with non-ASCII data: converting to ASCII
    ** R
    ** inst
    ** byte-compile and prepare package for lazy loading
    Error : .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: no suitable image found. Did find:
    /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries/libjvm.dylib: mach-o, but wrong architecture
    /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries/libclient.dylib: mach-o, but wrong architecture
    ERROR: lazy loading failed for package ‘eu.us.opendata’
  • removing ‘/Library/Frameworks/R.framework/Versions/3.5/Resources/library/eu.us.opendata’
    Installation failed: Command failed (1)

@kevinushey
Copy link
Contributor Author

The error suggests that rJava is looking for JDK 9, but you only have JDK 1.6 installed.

Please consider posting on https://community.rstudio.com if you need more help. You also likely need to run

sudo R CMD javareconf

to reconfigure rJava to use the appropriate JVM on your machine.

@jerrybreak
Copy link

I had the same issue with rJava and xlsx in both base R and Rstudio. It started to happen after I updated to Mac High Sierra 10.13.3. Not sure how it was ultimately solved but I followed the following steps more or less in this order:

Installed Java version suggested in:
https://support.rstudio.com/hc/en-us/community/posts/245270787-Error-in-library-xlsx-

Then, tried adding the line:
Sys.setenv(JAVA_HOME='C:\Program Files\Java\jre7') # for 64-bit version
before calling the library based on suggestion at:
https://www.r-statistics.com/2012/08/how-to-load-the-rjava-package-after-the-error-java_home-cannot-be-determined-from-the-registry/

At this point things still did not work.

Tried one suggestion in following page:
https://stackoverflow.com/questions/30738974/rjava-load-error-in-rstudio-r-after-upgrading-to-osx-yosemite
using what I think is an overwriting step in the terminal:
sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib

Still no better.

Lastly, I followed "kevinushey" suggestion above:
"@sanjmeh: have you installed a Java Development Kit? You need one if you want to use rJava. See http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html. I'd recommend using JDK 8 for now; I'm not sure if rJava supports the newer JDKs yet."

Installed Java Dev Kit. Then, ran "sudo R CMD javareconf" on command line. Re-started R base and Rstudio and the problem was fixed.

Thanks to all.

@EmbraceLife
Copy link

@jerrybreak Thanks, the following really works

Installed Java Dev Kit. Then, ran "sudo R CMD javareconf" on command line. Re-started R base and Rstudio and the problem was fixed.

@sanjmeh
Copy link

sanjmeh commented Oct 13, 2018

I had rJava working last month but now again the same error (Upgraded to High Sierra and played with .profile a bit during the month setting up NVIDIA - if that helps).

But this is a never-ending pain (rJava sucks).

After I ran sudo R CMD javareconf on the mac terminal, this is what I get. rJava still does not work.

Sanjays-iMac:bank-statements sm$ sudo R CMD javareconf
Password:
Java interpreter : /usr/bin/java
Java version     : 10.0.1
Java home path   : /Library/Java/JavaVirtualMachines/jdk-10.0.1.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.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home/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/jdk-10.0.1.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.1.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.

@Rmadillo
Copy link

I too had lots of probs and tried all of the above to no avail.

What worked for me (your milage will vary!) was doing this inside RStudio:

options("java.home"="/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/lib")

Sys.setenv(LD_LIBRARY_PATH='$JAVA_HOME/server')

dyn.load('/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/lib/server/libjvm.dylib')

library(rJava)

@kevinushey
Copy link
Contributor Author

Note that using Java 9 with R is not recommended -- you should prefer either Java 8 or the current version of Java (which appears to be Java 11.0.1). But Java 8 is more widely supported + tested.

@SubstantiaNegri
Copy link

I was experiencing the same issue loading rJava into Rstudio and tried all of the solutions suggested above with to no avail.

The solution that worked for me was the top answer suggested in this SO post. However, the key was the suggestion by Dave X to both link the libjvm.dylib to both /usr/local/lib/ and /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/.

After having linked libjvm.dylib to both places, I reopened Rstudio and rJava loaded just fine. This despite checking the options("java.home") and it returning NULL

@Dulani
Copy link

Dulani commented Jan 30, 2019

I had this problem too (OS X 10.12.6, R 3.5.0, RStudio 1.1.453, Java 1.8.0_144). I already had Java installed, I installed rJava, and then tried library(rJava). It resulted in the same namespace errors others had above, then a Google search, and ultimately landing on this page.

Then, I tried:

  1. sudo R CMD javareconf and the problem persisted.

  2. Restarting the R session in RStudio and the problem persisted.

  3. After reading other pages on the InterWebs, I opened a terminal, started R, and tried library(rJava). It worked PERFECTLY!
    {hmmm... he thinks}.

  4. So, I merely closed all of my RStudio instances and re-opened one. Then, library(rJava) worked just fine!!!

Not sure why, but it seems like a restart of RStudio was needed for me to get everything synced up.

@JackHo327
Copy link

All, I was able to repair the issue. I have High Sierra 10.13.5, and after updating to the latest Java update and running the "sudo R CMD javareconf" at the terminal prompt it corrected the issue in RStudio.

Same here.

> sessionInfo()
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

......

@qbao96xb
Copy link

I had the same issue with rJava and xlsx in both base R and Rstudio. It started to happen after I updated to Mac High Sierra 10.13.3. Not sure how it was ultimately solved but I followed the following steps more or less in this order:

Installed Java version suggested in:
https://support.rstudio.com/hc/en-us/community/posts/245270787-Error-in-library-xlsx-

Then, tried adding the line:
Sys.setenv(JAVA_HOME='C:\Program Files\Java\jre7') # for 64-bit version
before calling the library based on suggestion at:
https://www.r-statistics.com/2012/08/how-to-load-the-rjava-package-after-the-error-java_home-cannot-be-determined-from-the-registry/

At this point things still did not work.

Tried one suggestion in following page:
https://stackoverflow.com/questions/30738974/rjava-load-error-in-rstudio-r-after-upgrading-to-osx-yosemite
using what I think is an overwriting step in the terminal:
sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib

Still no better.

Lastly, I followed "kevinushey" suggestion above:
"@sanjmeh: have you installed a Java Development Kit? You need one if you want to use rJava. See http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html. I'd recommend using JDK 8 for now; I'm not sure if rJava supports the newer JDKs yet."

Installed Java Dev Kit. Then, ran "sudo R CMD javareconf" on command line. Re-started R base and Rstudio and the problem was fixed.

Thanks to all.

That's really work well to me.
Thanks much

@irudnyts
Copy link

I had the same issue with rJava and xlsx in both base R and Rstudio. It started to happen after I updated to Mac High Sierra 10.13.3. Not sure how it was ultimately solved but I followed the following steps more or less in this order:
Installed Java version suggested in:
https://support.rstudio.com/hc/en-us/community/posts/245270787-Error-in-library-xlsx-
Then, tried adding the line:
Sys.setenv(JAVA_HOME='C:\Program Files\Java\jre7') # for 64-bit version
before calling the library based on suggestion at:
https://www.r-statistics.com/2012/08/how-to-load-the-rjava-package-after-the-error-java_home-cannot-be-determined-from-the-registry/
At this point things still did not work.
Tried one suggestion in following page:
https://stackoverflow.com/questions/30738974/rjava-load-error-in-rstudio-r-after-upgrading-to-osx-yosemite
using what I think is an overwriting step in the terminal:
sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
Still no better.
Lastly, I followed "kevinushey" suggestion above:
"@sanjmeh: have you installed a Java Development Kit? You need one if you want to use rJava. See http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html. I'd recommend using JDK 8 for now; I'm not sure if rJava supports the newer JDKs yet."
Installed Java Dev Kit. Then, ran "sudo R CMD javareconf" on command line. Re-started R base and Rstudio and the problem was fixed.
Thanks to all.

That's really work well to me.
Thanks much

For me as well. I did the following steps:

  1. Install Java for macOS
  2. Install Java SE Development Kit 13
  3. Run sudo R CMD javareconf

@Kinroadzxc
Copy link

I had the same issue with the note:

 .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.6/Resources/library/rJava/libs/rJava.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.6/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.6/Resources/library/rJava/libs/rJava.so
  Reason: image not found

My mac is running with 10.14.6 Mojave, and I have both JDK 10.0.1 and JDK 11.0.4 installed.
Finally, I got out of this situation with a little trick.
And here's my suggested steps:

  1. Check if you have at least one version of JDK installed.
    In the terminal, usingjava -version
    If not, go to https://www.oracle.com/technetwork/java/javase/downloads/index.html and find a suitable version of JDK. My suggestion is JDK 11.
  2. Check if you have set the JAVA_HOME variable.
    In the terminal, usingecho $JAVA_HOME
    If there is a path, then you are fine.
    If not, run open .bash_profile and edit these text in
export JAVA_HOME=`/usr/libexec/java_home`

Or if you have multiple versions of JDK, just follow my example:

export JAVA_10_HOME=$(/usr/libexec/java_home -v10)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
alias jdk10='export JAVA_HOME=$JAVA_10_HOME'
alias jdk11='export JAVA_HOME=$JAVA_11_HOME'
export JAVA_HOME=`/usr/libexec/java_home`

Save it and run source .bash_profile to apply the change.
Restart the terminal and run echo $JAVA_HOME and you should be able to find the path listed.

  1. In the terminal, use sudo R CMD javareconf to reset R's java environment configuration.

  2. Then open your Rstudio, and in the Console run options("java.home"), if you get the answer with NULL, run the following: options("java.home"="/Library/Java/JavaVirtualMachines/jdk-11.0.4.jdk/Contents/Home") It's not suitable for other version of JDK so just paste your own java_home path in the quote.

  3. Steps above should solve your problem, but not mine.
    After those, I still encountered the issue, but I noticed that the error message
    /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib
    the version part 11.0.1 is not matching my JDK version 11.0.4.
    So here's the possible bug: the script form Rstudio, which is rJava.so, did not use the java path given, it detects your java version and loads the fixed version of JDK lib. And for JDK version 11, this is jdk-11.0.1.
    Then it's easy to deal with a little trick:
    Before you run library(rJava) in Rstudio's Console, temporarily change your jdk-version folder's name to 11.0.1 (if you are using jdk11). Other versions may also be fixed like this. Then it may work!
    And don't forget to change the folder's name back!

@qbao96xb
Copy link

qbao96xb commented Oct 9, 2019

Hi @Kinroadzxc, I run into this problem as well and after all, I agree that the problem might be caused by the rJava.so. Since all the way I try, even edit the .bash_profile or set up permanent JAVA_HOME path did not work.
Anyway many thanks for your suggestion of edit the Java Home folder name (y). I just think, how about change the current java version (mine is 13 as per some suggestion) into version 11.0.1?

After installing Java JDK 11.0.1, it works man :))))))))))

@woodydeck
Copy link

woodydeck commented Oct 16, 2019

@kevinushey This should be reopened since the most discussion is here, and there is a new twist in Catalina.

rJava will work fine in the terminal, but RStudio won't load rJava unless I specify a dyn.load of the jdk like: dyn.load('/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/jre/lib/server/libjvm.dylib')

The problem is that it isn't persistent, and no matter what I tried with the paths, or different jdk versions, it still errors every time I start RStudio fresh unless I do that. [Catalina is a mess and I'm rolling back. Lots of things not working. My cron jobs don't fire for R etc...sigh.]

@kevinushey
Copy link
Contributor Author

I'm not able to reproduce -- R (and RStudio) are both able to successfully load rJava. I did have to run

sudo R CMD javareconf

to make sure R was aware of my Java installation, though. I also have adoptopenjdk8 installed from Homebrew, which R was able to find and link to.

@woodydeck
Copy link

woodydeck commented Oct 17, 2019

I spent about six hours on it today. I upgraded to 15.0 Catalina from Mojava 10.14.6. Immediately I had segfaults on my package heavy and RAM intensive scripts. I have around 20 dependencies. sudo R CMD javareconf doesn't help besides getting it to run at all.

I updated to the latest RStudio from 1.1.463, but that just somehow installed 3.6 over my 3.5.2. My scripts only work with 3.5.2, so I played around and then set my path back to 3.5.2. It was still the same problem. I never figured out how to make rJava work without manual intervention, and the segfaults persisted even when I reinstalled Catalina and did this whole thing from scratch.

It's really annoying, but I wouldn't tear your head out trying to reproduce. There are a lot of variables. Just keep it in mind, because nobody who uses R should update to Catalina unless they like pain.

I hope future versions can improve the process. I found that the tool to switch R versions was 32 bit, so that made things even more fun. :-)

Edit: Forgot to say that I got segfaults exactly the same in RStudio and in the terminal, so it's probably not RStudio causing those.

@ronblum
Copy link
Contributor

ronblum commented Oct 17, 2019

@woodydeck An aside: there's a new 64-bit RSwitch app at https://github.com/hrbrmstr/RSwitch/tree/master/releases . We're not "officially" stating support, but I've been using it with success. And in general, the author is very involved with R and RStudio.

@kikicova
Copy link

I was experiencing this issue on macOS 10.15, R 3.6.1, rJava 0.9-11, RStudio 1.2.5019. After many hours of languish, I finally discovered a working solution here: rJava #151.

I ran the below from the terminal:

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

I was then able to load rJava successfully in RStudio.

@fabern
Copy link

fabern commented Dec 10, 2019

I experienced the same issue as @stuartsoorholtz on macOS 10.14.6, R 3.6.1, rJava 0.9-11, RStudio 1.2.5019.
Java 8 was installed with brew using brew cask install adoptopenjdk8, but earlier I had a newer version of Java, which might have lead to the wrong library path described below.

Like @stuartsoorholtz, I resolved the issue by modifying rJava.so in the R.framework folder.

Initially, in R started from the terminal library(rJava) worked, in RStudio it didn't with the usual error message:

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.6/Resources/library/rJava/libs/rJava.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.6/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.6/Resources/library/rJava/libs/rJava.so
  Reason: image not found

The error message tells us, that it tries to load the library from a folder /jdk-11.0.1.jdk/.
Looking at the content of rJava.so in the R.framework folder with otool -l gives a long list with an entry for jdk-11.0.1.jdk somewhere in between:

$ otool -l /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so
[...]
Load command 10
          cmd LC_LOAD_DYLIB
      cmdsize 112
         name /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib (offset 24)
   time stamp 2 Thu Jan  1 01:00:02 1970
      current version 1.0.0
compatibility version 1.0.0
[...]

Now, we can modify this path in rJava.so with the correct one with install_name_tool:

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

After that, RStudio could load the rJava package.

@alexsanjoseph
Copy link

alexsanjoseph commented Dec 12, 2019

For me on Ubuntu Server 18.04,
sudo R CMD javareconf
ran fine.

What worked was adding
dyn.load('/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/libjvm.so')
before loading the library
library(rJava)
which worked fine

@abbeythorpe
Copy link

I had the same issue on mac OS 10.14.6, R3.5.1.

I followed the same steps as @fabern (followed instructions from here: http://www.owsiak.org/r-java-rjava-and-macos-adventures/) but then had an additional error with -fopenmp

clang: error: unsupported option '-fopenmp'
make[2]: *** [libjri.jnilib] Error 1
make[1]: *** [src/JRI.jar] Error 2
make: *** [jri] Error 2
ERROR: compilation failed for package ‘rJava’

I had to change gcc to version gcc@7 which I did by following the comment here, beginning at "Error (unsupported option '-fopenmp)":
https://stackoverflow.com/a/51996290
I restarted R and now everything seems to be working.

@jerrybreak
Copy link

Here I am again updating from macOS Sierra (see above comment) to now Catalina 10.15.2. At the same time I also updated to Rstudio 1.3.776. Then, rJava failed to load in base R and Rstudio but not in the terminal.

The following was the error from base R and Rstudio:

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.6/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

So, I don't remember the exact order of events but after a couple of days of going nuts trying to fix this I ended up fixing it (I think). From the start I ran the almighty "sudo R CMD javareconf" and it was complaining about an error. Sorry I no longer have the error in my terminal.

Then, I went ahead and downloaded java JDK version 11.0.1 (I had 11.0.6) because that was exactly the version under JavaVirtualMachines/ the error message was specifying above. I downloaded it from here

Nothing was working yet but I realized that the java -version being used was 11.0.6, so I decided to remove that version from the JavaVirtualMachines/. So, I sudo rm -rf jdk-11.jdk according to
this

Then again the sudo R CMD javareconf. I think at this point I was able to load rJava and xlsxjars in both base R and R, but now xlsx was making R crash upon loading the library. Even on the terminal it made it crash. A fatal error occurred type of message that was only available from the terminal.

Error occurred during initialization of VM Unable to load native library:

At some point in the series of events above, I also realized (because some other thread and some other error) that I did not have the most up to date version of Xcode, so I went to the app store and updated it to the suggested version.

I think I was getting the following error Error: Can't run /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/ibtool (no such file)

when trying to use something similar to what @stuartsoorholtz said above:

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

But modified to the jdk versions I had. So, after updating Xcode, I continued to get the same error so I think that didn't really work for me.

So, by now rJava was loading ok now but xlsx was not. I was about to give up using xlsx but then I decided to install the most recent version of JDK 13.0.2 from, then re-ran the java reconf command on the terminal and it worked!! I was able to load xlsx.

So, in summary, I think I needed the JDK 11.0.1 version first to be able to make the java--R connection work (somewhere in some thread I read apple no longer supports some sort of automatic link that communicates with Java). Then, once I was able to load rJava but not xlsx, I think I needed JDK 13.0.2 and reconfigure java to make xlsx work. Why? I wouldn't know.

One odd thing that happened the very first time I actually used the read.xlsx() was the following warning:

data15 <- read.xlsx ( “file.xlsx” , 1 ) WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.apache.poi.util.SAXHelper (file:/Library/Frameworks/R.framework/Versions/3.5/Resources/library/xlsxjars/java/poi-ooxml-3.10.1-20140818.jar) to method com.sun.org.apache.xerces.internal.util.SecurityManager.setEntityExpansionLimit(int) WARNING: Please consider reporting this to the maintainers of org.apache.poi.util.SAXHelper WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release

So, I don't know if the warning was kicked in by java or by R. But then, the xlsx file loaded fine and subsequent use of xlsx in new R sessions have not spit out the same warnings. By the nature of the warning I think I may have done something "illegal" but I guess I don't know enough about this stuff. Anyway rJava and xlsx work for me now.

By the way, at some point during my suffering I also uninstalled and reinstalled rJava so I don't know if that also helped or not. I was using install.packages('rJava', dependencies=TRUE, repos='http://cran.rstudio.com/') because I had a mess with the repositories in base R.

Good luck. I don't think I'll ever upgrade to a new macOS version again.

@saudiwin
Copy link

You can also just copy the jdk folder to the one Rstudio wants to see. That worked for me.

@jerrybreak
Copy link

@saudiwin do you mean copy the jdk directory into /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/ ?

@saudiwin
Copy link

No just copy the jdk library that's actually installed in /Library/Java/JavaVirtualMachines/ to the path that Rstudio is looking for (apparently some different version of the jdk in the same folder). I'm sure you could find a way to do with symbolic links too if you want to get fancy.

This strategy is a hack and I'm sure it could break down at points as the versions of Java aren't actually the same. But given that there's no direct solution proposed yet, I thought I would post.

@hhuang728
Copy link

Hi @Kinroadzxc, I run into this problem as well and after all, I agree that the problem might be caused by the rJava.so. Since all the way I try, even edit the .bash_profile or set up permanent JAVA_HOME path did not work.
Anyway many thanks for your suggestion of edit the Java Home folder name (y). I just think, how about change the current java version (mine is 13 as per some suggestion) into version 11.0.1?

After installing Java JDK 11.0.1, it works man :))))))))))

You made my day!!!!!!!
It finally works!

@arkal
Copy link

arkal commented Mar 12, 2020

FWIW I tried R CMD javareconf but it only worked using R in a terminal but not in Rstudio for some reason.

I'd get the error

> 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 '/usr/local/lib/R/3.5/site-library/rJava/libs/rJava.so':
  dlopen(/usr/local/lib/R/3.5/site-library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
  Referenced from: /usr/local/lib/R/3.5/site-library/rJava/libs/rJava.so
  Reason: image not found

@fabern's solution worked for me, however i had to find libjvm.dylib first since i didn't have the location directly as in his example (I had @rpath)

I identified the location of libjvm.dylib was $(/usr/libexec/java_home)/lib/server/libjvm.dylib for my version of java (openjdk version "13.0.2" 2020-01-14)

install_name_tool -change \
             "@rpath/libjvm.dylib" \
             /Library/Java/JavaVirtualMachines/openjdk-13.0.2.jdk/Contents/Home/lib/server/libjvm.dylib \
            /usr/local/lib/R/3.5/site-library/rJava/libs/rJava.so

And now rJava works on both terminal and RStudio.

@Aariq
Copy link

Aariq commented May 5, 2020

I had this problem on macOS 10.14.6 with R version 3.6.1 and RStudio version 1.2.1578. I tried just about everything in this thread, but eventually solved it simply by installing jdk 11.0.1, which seems like what rJava requires, and then running sudo R CMD javareconf in the terminal. Would be nice to see a more concise error message like "rJava requires jdk version 11.0.1 to run. Install it from "

@dtgossi
Copy link

dtgossi commented Jun 27, 2020

I'm having problems installing rJava

library(rJava)
Unable to find any JVMs matching version "(null)".
No Java runtime present, try --request to install.
Error: package or namespace load failed for ‘rJava’:
.onLoad failed in loadNamespace() for 'rJava', details:
call: fun(libname, pkgname)
error: JVM could not be found
In addition: Warning messages:
1: In system("/usr/libexec/java_home", intern = TRUE) :
running command '/usr/libexec/java_home' had status 1
2: In fun(libname, pkgname) :
Cannot find JVM library 'NA/lib/server/libjvm.dylib'
Install Java and/or check JAVA_HOME (if in doubt, do NOT set it, it will be detected)

I don't know what to do. I'm on MacOsMojave.

The broader context of what I'm trying to do is to get the Stanford NLP library to work

install.packages("coreNLP")
library(coreNLP)
downloadCoreNLP()

@dapritchard
Copy link

This solution from @hmorzaria worked for me (thank you!). However, I'll just add that in the case of RStudio Server, restarting the R session alone did not work, I had to restart RStudio Server itself via the command sudo rstudio-server restart.

I had the same issue in a Ubuntu server running 18.04 and RStudio server. I tried all the solutions in this thread. None worked.
Instead, I installed JDK 8 and followed the "Setting the JAVA_HOME Environment Variable" according to
https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-on-ubuntu-18-04
Then specified:
sudo R CMD javareconf JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
as here:
https://stackoverflow.com/questions/42562160/r-cmd-javareconf-not-finding-jni-h

@ronoevan
Copy link

ronoevan commented Jan 22, 2021

Finally it worked for me:

  1. Installed Java downloaded from https://support.apple.com/kb/DL1572?locale=en_US

  2. Installed JDK downloaded from https://www.oracle.com/java/technologies/javase-3.

  3. From the terminal, I run the code: sudo R CMD javareconf

Then it prompted me to put my computer password. Then it configured it successfully.

  1. Finally my rJava successfully run in my R/RStudio.

Best wishes.

Evans

@Kodwings22
Copy link

You can check this link too may be useful ( https://kodlogs.net/306/rjava-could-not-be-loaded)

@jcutlerbiostats
Copy link

jcutlerbiostats commented Jun 28, 2023

I have the exact same problem loading rJava:

Error: package or namespace load failed for ‘rJava’:
.onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(jli, FALSE)
error: unable to load shared object '/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jli/libjli.dylib':
dlopen(/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jli/libjli.dylib, 0x000A): tried: '/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jli/libjli.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jli/libjli.dylib' (no such file), '/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jli/libjli.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))

This is after installing Java, installing JDK, running sudo R CMD javareconf in the terminal, and starting up RStudio and trying to load the rJava library.

In short, this problem still exists in 2023 even after all this big long discussion and all these detailed pointers!

UPDATE: I just solved my problem! I installed the wrong JDK. The first time, I installed ARM64 Compressed Archive. I needed to install ARM64 DMG Installer for some reason. At least, when I tried installing that one, and typing sudo R CMD javareconf in the terminal, the output from running that command was completely different than the questionable output I kept getting before, and when I opened RStudio afterwards, rJava simply loaded, no problem.

@ronblum
Copy link
Contributor

ronblum commented Jun 30, 2023

@jcutlerbiostats I'm glad to hear that this resolved the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests