42

I can load the rJava and xlsxjars packages, but I get a "Failed to load JVM" when I execute library(xlsx). Below are my details.

OS Version:

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11
BuildVersion:   15A284

Java Version:

$ java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)

Java Path:

$ which java
/usr/bin/java

Java Home:

$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home

R Version:

$ R --version
R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)

How I launch R:

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

Loading the rJava and xlsxjars packages successfully:

> library(rJava)
> library(xlsxjars)

Unsuccessfully trying to load the xlsx package:

> library(xlsx)
JavaVM: requested Java version ((null)) not available. Using Java at "" instead.
JavaVM: Failed to load JVM: /bundle/Libraries/libserver.dylib
JavaVM FATAL: Failed to load the jvm library.
Error : .onLoad failed in loadNamespace() for 'xlsx', details:
  call: .jinit()
  error: JNI_GetCreatedJavaVMs returned -1
Error: package or namespace load failed for ‘xlsx’

R Configuration:

Matts-MacBook-Pro:~ matt$ R CMD javareconf
Java interpreter : /usr/bin/java
Java version     : 1.8.0_65
Java home path   : /Library/Java/JavaVirtualMachines/jdk1.8.0_65.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 OS X

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_65.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/../include/darwin -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include    -fPIC  -Wall -mtune=core2 -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_65.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_65.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
Updating Java configuration in /Library/Frameworks/R.framework/Resources
2
  • What gives R CMD javareconf?
    – user3710546
    Feb 3, 2016 at 14:03
  • 1
    Maybe the read_excel function from the readxl package might work as an alternative?
    – ekstroem
    Feb 4, 2016 at 14:37

5 Answers 5

62

Unfortunately, these symlink tricks didn't help me. However, I found a similar issue on stackoverflow! (where else ;)) This one worked for me by doing the following:

  1. In terminal enter the following to relink Java correctly:

    sudo R CMD javareconf
    
  2. Back in R (or RStudio) install the rJava package from source:

    install.packages("rJava",type='source')
    

Hope, this helps!

5
  • 8
    I had to do this afterwards for it to work: stackoverflow.com/questions/30738974/… Sep 30, 2016 at 15:41
  • works after adding the link creation line by Tobias Mini
    – slymore
    Dec 10, 2016 at 23:24
  • Per the link pointed to by ndimhypervol, I also had to run the following command sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
    – Martin
    Feb 25, 2017 at 21:10
  • In step #2, needed to run as sudo R from terminal, and then install.packages(...)
    – avriis
    Apr 28, 2017 at 13:10
  • did not work for me: configure: error: One or more JNI types differ from the corresponding native type. You may need to use non-standard compiler flags or a different compiler in order to fix this. ERROR: configuration failed for package ‘rJava’ Dec 12, 2017 at 15:55
14

I ´ve got also problems with loading packages like openNLP, xlsx, RWeka, ... and got the same error message like you. But finally this command worked for me on OS X 10.11.3: sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib

1
  • 2
    xlsx not working for me on 10.12 Sierra. If I run that command it returns ln: /usr/local/lib/libjvm.dylib: File exists but I'm still getting the error
    – Dambo
    Nov 9, 2016 at 21:12
2

There might be a problem in the version of Java for which the R xlsx package was built in. Some old software require to install the Java 6 for OS X 10.11 El Capitan, OS X 10.10 Yosemite, OS X 10.9 Mavericks, OS X 10.8 Mountain Lion, and OS X 10.7 Lion. So you can try to install this old version of Java (Java for OS X 2015-001) at Apple website, link below: https://support.apple.com/kb/DL1572?locale=pt_BR&viewlocale=en_US

And check if you have a Java SE Development Kit 8. You can download it at Oracle website, link below: www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

I was having the same problem and downloading the old version of Java solved it.. I hope this help you!

0
1

Basically, just do what is described here: https://oliverdowling.com.au/2015/10/09/oracles-jre-8-on-mac-os-x-el-capitan/

short version: d/l Java JRE from oracle as tar file http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html

then put this into you terminal (for sudo, your pw is needed):

cd ~/Downloads
tar -xzf `ls -1r jre-*-macosx-x64.tar.gz | head -1`
cd `ls -1rd jre*/ | head -1`
defaults write `pwd`/Contents/Info.plist JavaVM -dict-add 'JVMCapabilities' '<array><string>JNI</string><string>BundledApp</string><string>CommandLine</string></array>'
plutil -convert xml1 Contents/Info.plist
sudo mv `pwd` /Library/Java/JavaVirtualMachines/
mkdir -p Contents/Home/bundle/Libraries
cd Contents/Home/bundle/Libraries
ln -s ../../lib/server/libjvm.dylib libserver.dylib

works fine on my OS X 10.11

5
  • 6
    be very, very careful with this one -- that "sudo mv pwd /Library/Java/JavaVirtualMachines" line is going to move everything in pwd to that Java folder...
    – Andrew
    Mar 7, 2016 at 17:30
  • 4
    So I did this... and it crashed my computer of course, moving all my files into the JVM folder. How do I 'undo' it? Warning to all others: DON'T DO THIS!! Mar 28, 2016 at 22:37
  • 1
    Why mv pwd rather than mv .?
    – Olathe
    Mar 29, 2016 at 2:06
  • this is dangerous, you can break your system Sep 6, 2016 at 3:33
  • This worked for me (using macOS Sierra Version 10.12, macbook pro 15-inch mid 2015 w/ 16GB ram - for anyone who has the same device) Nov 30, 2016 at 18:51
0

For me, MacOS Catalina 10.15.7, it was as simple as installing the current JRE and JDK software (downloaded as .dmg files from the official distributer websites on java.com and oracle.com - No affiliations whatsoever!).

I have also (re-) installed the rJava package, but I don't feel this was necessary. I actually simply did not have Java installed on my computer.

Now the xlsx package loads.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.