125

I recently "upgraded" from OSX Mountain Lion to Yosemite and from R 3.1.3 to 3.2. Immediately after the upgrade, when I opened R or RStudio I got a pop-up message saying that I needed to install Java 6. In addition, loading rJava or any package that depends on rJava (e.g., xlsx) caused RStudio to crash (R also crashed when I tried this by opening R.app directly).

After trying a few fixes found on Stack Overflow and elsewhere (more details below), I am at a point where loading rJava or any package that depends on rJava no longer causes R to crash, but results in the following error:

library(rJava)
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.2/Resources/library/rJava/libs/rJava.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rJava/libs/rJava.so
  Reason: image not found
Error: package or namespace load failed for ‘rJava’

However, if I invoke R from the command line and then load rJava or any package that depends on rJava, it seems to work (or at least I don't get any error messages).

I've tried a number of different attempted fixes, some of them a few times, and can't quite remember exactly what I did in what order (didn't realize this would be such a morass and wasn't really keeping track), but here's the gist of it:

  • Added the following to my .bash_profile (per this SO answer):

    export JAVA_HOME="/usr/libexec/java_home -v 1.8"
    export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/server

  • Reconfigured java from the command line as follows:

    sudo R CMD javareconf -n

  • Checked options("java.home") and discovered this was set to NULL. I tried setting it to the following (per this SO question):

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

  • Installed the latest Java Development Kit and reinstalled rJava from source (can't remember where I found that one).

At some point while trying all of these, I was able to load rJava without crashing R, but instead got the error message posted above. In addition, when I quit RStudio, it would seem to close normally, but then an "RStudio quit unexpectedly" message would pop up, indicating that the program had crashed while trying to close.

I finally decided to install Java for OS X 2014-001 (Java 6), as I seemed to be running out of options. Now, when I opened R or RStudio the "This software needs Java 6" pop-up message no longer appeared. However, I was still getting the .onLoad failed in loadNamespace() for 'rJava' error message posted above.

In reviewing some of the posts I'd already looked at, I noticed another SO answer that I'd missed before, which recommended opening RStudio with the following command line code that gives RStudio the correct path to java:

LD_LIBRARY_PATH=$(/usr/libexec/java_home)/jre/lib/server: open -a RStudio

That opened an RStudio window and I was also able to load rJava and packages that depend on it without getting an error.

Lastly, I tried running R from the command line (which I hadn't done before). It turns out that on the command line, loading rJava or any package that depends on rJava works and does not throw any errors.

So, I can now get rJava to work if I open RStudio from the command line with the code that gives RStudio the java path (as noted above). However, I'd like to find a way to fix the underlying problem, whatever it may be, so that RStudio can be opened in the usual Mac way, without needing a command line kludge. I'm also concerned that having an old version of Java installed could cause problems down the road.

Does anyone have any ideas about how to diagnose and solve this issue?

Alex Popov
  • 3,726
  • 1
  • 17
  • 20
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Does this solution still work for you in El Capitan? I got this working before using the `LD_LIBRARY_PATH=$(/usr/libexec/java_home)/jre/lib/server: open -a RStudio` trick, but it does not work any more. I've [wrote a blog post regarding this issue](http://paulklemm.com/zenf/blog/2015/02/20/run-rjava-with-rstudio-under-osx-10-dot-10/) and described this problem there. I wonder if others experience this problem too in this configuration or if it is my fault. – Paul Klemm Dec 09 '15 at 16:15
  • I'm still using `Yosemite`, so I'm not sure. See below for an answer related to `El Capitan`. – eipi10 Dec 09 '15 at 16:24
  • Ok, I think I will post a new question then. Yes the `El Capitan` solution below is helpful as soon as `RStudio` loads `rJava` as expected. – Paul Klemm Dec 09 '15 at 16:39
  • 2
    It worked after I restarted RStudio – Pranay Aryal Dec 25 '16 at 18:36
  • On diagnosing, try `otool -L /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so` to get a list of the shared libraries and how they get resolved. I'm not sure how to discover the runtime contents of `@rpath`. – Dave X Nov 02 '17 at 15:48

18 Answers18

280

I had the same problem and went through the same steps as you. The final step to allow starting RStudio through Finder/Spotlight was to link libjvm.dylib to /usr/local/lib:

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

-f flag is added to force overwriting existing file/link

Alex Popov
  • 3,726
  • 1
  • 17
  • 20
  • 4
    Worked to fix the same problem when the upgrade from El Capitan to Sierra is done. +1 – Murta Sep 21 '16 at 17:43
  • 2
    This solved a lot of problems; especially if original error comes from `cannot jfindclass[[1]]` that may come from rJava and a JDBC driver – Jenks Jan 31 '17 at 20:39
  • 1
    I have to run this command every time I update Java on my Macbook Pro. It still works on macOS Sierra 10.12.5 and Java Version 8 Update 141. – thiagoveloso Jul 19 '17 at 08:37
  • This answer is still relevant, and works today for OSX Sierra 10.12.6 – Shanemeister Sep 25 '17 at 16:36
  • 11
    This almost worked for me. Additionally I had to add a symlink in the R framework to the `/usr/local/lib/libjvm.dylib` file, since /usr/local/lib/ doesn't seem to be on the @rpath. For example: `ln -s /usr/local/lib/libjvm.dylib /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/` – Dave X Nov 02 '17 at 14:23
  • I ran this command on MacOS Sierra 12.6 but no good. The terminal asks for my administrative password but doesn't return any message R keeps not loading rJava. Why? – Lucas De Abreu Maia Nov 02 '17 at 20:15
  • make sure you are using 'sudo' and '-f' as shown in the asnwer above – Alex Popov Nov 04 '17 at 22:33
  • Should be noted that as a package developer I had to additionally use @dave-x 's answer to get this to work in a RStudio "Check". – Davis Vaughan Feb 08 '18 at 15:57
  • Works in 10.13.3. Just had to change the path to libjvm.dylib, as I don't have the jre directory in my java_home. Also, I had to set an environment variable from inside RStudio: Sys.setenv(JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home') – chao Feb 21 '18 at 14:45
  • It worked for me (10.12.6 Sierra). I had to restart Rstudio to take the new soft link into account – Boris Mar 22 '18 at 18:45
  • 1
    This can be a useful https://github.com/snowflakedb/dplyr-snowflakedb/wiki/Configuring-R-rJava-RJDBC-on-Mac-OS-X – user2079550 May 31 '18 at 17:44
53

On OSX El Capitan 10.11, the user doesn't have permission to write to /usr/lib. So instead, use the following command:

sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
lalas
  • 763
  • 1
  • 7
  • 10
29

You can load the libjvm.dylib explictly, by run like

dyn.load('/Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home/jre/lib/server/libjvm.dylib')
library(rJava)
yuan
  • 391
  • 3
  • 2
  • Possible for sure, but not convenient. The accepted answer is probably a better option. `+1` for offering an alternative though. – thiagoveloso Jul 19 '17 at 08:35
27

On macOS High Sierra (10.13.1) and Java Version 9 you have to use a slightly different JVM path (notice the missing jre folder in the path compared to the instructions for earlier Java versions):

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

You also have to notify R about the JVM:

MY_R_VERSION=$(Rscript -e "cat(with(R.version, sprintf('%s.%s', major, substring(minor, 1, 1))))")
ln -s /usr/local/lib/libjvm.dylib /Library/Frameworks/R.framework/Versions/$MY_R_VERSION/Resources/lib/
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
Endre
  • 690
  • 8
  • 15
17

Following command works:

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

After that, in RStudio, loading rJava works through loading of the "xlsx" package.

library("xlsx")

P.S. #1 Environment: Mac OS X El Capitan 10.11.3+ with RStudio 0.99.491+ and R 3.2.3+. (I've now tested this also on macOS Sierra (10.12) and R.3.3.1.)

P.S. #2 I find that openxlsx is much faster and doesn't rely on Java to work, so I'm now using that package. Hope that helps others.

  • 1
    thank you my friend, you saved my day. I tried with other solutions found around but this was the one that solved my issue. – Matteo De Felice Mar 11 '16 at 12:55
  • 1
    Another El Cap user (but using the MacGUI) with a vote of thanks. Tried this since first since it appears simplest (and earlier efforts with directions for installing Java runtime 1.6 had not solved the problem) and immediate success with pkg:rJava and dependency pkg;XLConnect. May need to install the legacy Java 1.6 that is available from Oracle and linked from the Apple page. – IRTFM Jun 16 '16 at 16:39
  • 1
    Hi, your advice works also with MacOS High Sierra, thank a lot!! – user8193079 Jan 24 '18 at 14:28
  • 1
    `openxlsx` FTW! – iled Oct 30 '18 at 15:40
  • Yes! @iled: I agree, better use `openxlsx` anyway. – Sander W. van der Laan Nov 15 '18 at 08:44
7

You should be able to use the CRAN rJava, without needing to recompile rJava or do any additional steps by linking the shared library to the R frameworks library directory.

sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /Library/Frameworks/R.framework/Libraries
Jim
  • 4,687
  • 29
  • 30
6

This is how I configured it working properly on two Macs running Mac OS X El Capitan (10.11.6):

  1. Uninstall 'rJava' by issuing the following commands in a terminal window:

    Rscript -e 'remove.packages("rJava")'
    sudo Rscript -e 'remove.packages("rJava")'
    
  2. Download and install the Java software from Oracle: https://www.java.com/en/download/mac_download.jsp

  3. Add the following lines to /Users/<userid>/.bashrc using your favorite editor:

    # Set JAVA_HOME so rJava package can find it
    export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)/jre
    
  4. Close and restart any terminal, R, and RStudio windows (to pick up the changes to .bashrc).

  5. Run the following command in a Terminal window:

    sudo ln -sf $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
    
  6. Run the following command in a Terminal window:

    sudo Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'
    
  • I am getting licuuc library/linker error while following this, are you able to explain? Trying to install on macOS Sierra, more [here](https://stackoverflow.com/q/45131343/164148) (Earlier in El Capitan). – hhh Jul 16 '17 at 20:48
5

Here is the easy steps for it:

  1. remove the rJava package: remove.packages(rJava)
  2. close R
  3. install latest Java on you mac
  4. open terminal and type this command: sudo R CMD javareconf
  5. Open R and install rJava with this command:

    install.packages("rJava", dependencies=TRUE, type="source")

slfan
  • 8,950
  • 115
  • 65
  • 78
  • I actually did this before I ended up here. It made my rjava dependent library install but not start. The linking of `libjvm.dylib` into `/usr/local/lib` made it also start :) – jonalv Nov 14 '16 at 15:58
3

The image not found issue exists with a brand-new install on OSX High Sierra with the latest Java SE 10 JDK.

I was able to solve the path issue with rJava using the fix found on the rJava Github issues page: https://github.com/s-u/rJava/issues/78

R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers

Galen
  • 31
  • 1
1

Here what worked for me on MAC:

  1. in your ~/.profile or ~/.bashrc add this line: %export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home

    1.1 % source .profile (or % source .bashrc)

  2. % sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
  3. remove.packages(rJava)
  4. remove.packages(Rweka)
  5. From the terminal enter this command: %sudo R CMD javareconf
  6. install.packages("rJava", dependencies = TRUE, type = "source")
  7. install.packages("rJava", dependencies = TRUE, type = "source")
  8. Load your library("rJava", "RWeka")

Best of luck.

kamwo
  • 1,980
  • 1
  • 23
  • 32
Nasser
  • 11
  • 1
1

Maybe another simple answer which does not touch your filesystem:

$ install_name_tool -add_rpath /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/server /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/libs/rJava.so

(Substitute jdk1.8.0_131.jdk with your JDK path.)

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
mokjpn
  • 11
  • 1
  • 2
    I have homebrew R, and the exact command that worked for me is: `install_name_tool -add_rpath /Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home/jre/lib/server /usr/local/lib/R/3.3/site-library/rJava/libs/rJava.so`. I was able to see that the rpath was missing with `otool -l /usr/local/lib/R/3.3/site-library/rJava/libs/rJava.so | grep LC_RPATH` – Ian Jun 05 '17 at 18:19
  • @Ian perhaps you know a solution to this [here](https://stackoverflow.com/questions/45131343/rjava-linker-error-licuuc-when-trying-to-install-on-macos-el-capitan-10-11-6) where I have installed Java with Homebrew and now trying to get rJava working. Ideas? – hhh Jul 16 '17 at 20:32
1

Check the location of libjvm.dylib file.

Try this, in my case this worked:

dyn.load('/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home/lib/server/libjvm.dylib')
library(rJava)
baduker
  • 19,152
  • 9
  • 33
  • 56
ishonest
  • 433
  • 4
  • 8
1

I went through all the troubleshooting I could find, then installed jdk-11.0.1_osx-x64_bin.dmg from: Oracle downloads

Everything worked perfectly after that.

Check the version of the library that was not loaded when you run library('rJava') and match it to the java version you need installed.

iainhunter
  • 487
  • 4
  • 10
1

I noticed that 'rJava' is pretty strict about JDK version while loading in Rstudio.
In my case (Mac OS. 10.14.6 - Mojave) I had installed jdk-13 which was tested against jdk-11.
I have linked my JDK version to a non-existent directory that was assumed to be valid by Rstudio (JDK 11):

sudo ln -sf /Library/Java/JavaVirtualMachines/jdk-13.jdk/ /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk

and suprisingly it works like a charm...
BTW., there was no problem to run 'rJava' following the previous instructions in native R.

speleo
  • 123
  • 7
0

Here you can download the legacy Java version 6 for El Capitan: https://support.apple.com/kb/DL1572?viewlocale=en_US&locale=en_US After doing so, the rJava package works for me.

ignazw
  • 129
  • 1
  • 6
0

This line below has solved the same problem I was having with rJava package as some others in this discussion did. I'm sure there are more than one solution to this problem and I sincerely thank them all for their contributions because sometimes one line like the one below saves a lot of time from going to trash!

sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
0

Run the following on the command line: sudo R CMD javareconf

Several solutions above have mentioned this, but they also suggest that one remove and subsequently re-install the rJava package. I found those additional steps to be unnecessary.

Megatron
  • 15,909
  • 12
  • 89
  • 97
  • I tried your solution `sudo R CMD javareconf` but the `library(rJava)` still gives the same errors: `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': ... Reason: image not found` – Lazarus Thurston Oct 13 '18 at 10:23
0

You can change the reference in R by using the following command:

sudo install_name_tool -change /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home/lib/server/libjvm.dylib /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home/lib/server/libjvm.dylib /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rJava/libs/rJava.so
4b0
  • 21,981
  • 30
  • 95
  • 142
Si Kh
  • 1