Hey Mac OSX users with Java 1.8 installed. Did R just request a Java 1.6 installation and then promptly crash your session? If so, read on…
The Problem
A few days ago I was attempting to use the mallet package for topic models and I found that typing
> library(mallet)
caused two things to happen:
- The operating system launches a dialog asking whether I would like to download Java 1.6
- The R session crashes (regardless of what I did about the dialog)
This seems to be a general problem for packages that depends on the rJava package.
I’m running the latest Mac OSX (Yosemite 10.10.1) and have installed the latest Java
$ java -version java version "1.8.0" Java(TM) SE Runtime Environment (build 1.8.0-b132) Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
and the latest R
$ R --version R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet" Copyright (C) 2014 The R Foundation for Statistical Computing Platform: x86_64-apple-darwin13.4.0 (64-bit)
But for some reason with R, it’s Java 1.6 (which is no longer even being updated) or bust.
The fix (maybe)
The trail of bug reports starts here, but before you delve into those and bother Simon Urbanek as much as I did, try the following:
- Download and install Apple’s Java version 1.6 like you were asked to.
- Reconfigure your R installation by typing
sudo R CMD javareconf
in a Terminal window. (Thanks to commenter Benjamin for pointing out that there shouldn’t have been a -n flag in this line)
- Trigger a recompile by reinstalling rJava by typing
install.packages('rJava', type='source')
Now if you relauch R and load your package it should use your shiny new Java 1.8 and not crash any R sessions.
[Late addition] For this to work you may have to set (or have something else set) some environment variables too. See the comments below for my settings.
Thanks, Will.
I followed the steps but found
sudo R CMD javareconf -n
didn’t set 1.6 as the default Java for R.You can point R to 1.6’s direction by setting environment variables, like
rJava
can be loaded without any trouble after these.Cheers,
Rich
I was trying to get R to work with 1.8 not with the 1.6. When you followed the instructions did
library(rJava)
still fail, or did it just give you java 1.8?Your comment is reminding me that I have the lines
in my
~/.bash_profile
.Weirdly, launched from the command line
but from inside RStudio
(still works in RStudio though.)
If you look closely, you should actually run
that is, without the
-n
flag, which triggers a dry run and prevents any actual configuration happening.You may have to fix the file permissions afterwards:
This worked for me. I am on os x yosemite and rJava problem when I tried to install RDAVIDWebservice. After following this step (and changing the permission as suggested), I was able to install it without problem. Thank you!
Hello, thanks for writing this post…it’s one of the first that comes up when Googling for “rJava” and Yosemite…for future travelers, you might want to edit your post to reflect Benjamin’s comment:
That -n flag really threw me off for about an hour.
Sorry, for some reason the code snippets didn’t go through. One more try.
Hi Will, I went through your instructions step-by-step to no avail. Worse, now rJava will no longer install. Hours later I still have no clue what is going on. I notice Sys.getenv(‘JAVA_HOME’) and Sys.getenv(‘LD_LIBRARY_PATH’) both return “” in R and RStudio. Below is the output from the reconfiguration showing the true ‘JAVA_HOME’ path. Any ideas? Thanks in advance.
Do you get the same result launching RStudio from the Finder as you do when you launch it from Terminal? That is, by typing: open -a RStudio
Hi, I followed the instruction listed and set the env variables JAVA_HOME and LD_LIBRARY_PATH. However, when running the last step which install.packages(‘rJava’, type=’source’), I got an error that
Any suggestion for solution?
I am using the same configuration of R and Java. Below is the screen shot of error
The best and more complete explanation of how to get this to work was found here:
http://paulklemm.com/blog/2015/02/20/run-rjava-with-rstudio-under-osx-10-dot-10/
This person may have used this as a resource, but they have all the steps clearly documented.
The blog post moved to here: http://paulklemm.com/zenf/blog/2015/02/20/run-rjava-with-rstudio-under-osx-10-dot-10/
The (updated) info here worked for me on osx 10.12.4, R 3.4.0
http://paulklemm.com/blog/2017-02-12-fix-onload-failed-rjava-macos/
Thank you for getting me started solving this problem on El Capitan (10.11). While I was unable to use exactly what you had here. The following allowed me to install rJava successfully.
JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home \
JAVA_CPPFLAGS=-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/ \
r CMD javareconf
From StackOverflow: On OSX El Capitan 10.11, the user doesn’t have permission to write to /usr/lib. So instead, use the following command:
1000 kudos. This worked like a charm.
Tnx! This did the trick for me – I was getting
unable to load shared object ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/rJava/libs/rJava.so’.
while loading rJava or any package that depends on it – and this linking solved the problem.
Thanks!
You are awesome! Thanks a million.
Hi, Jack, I gave this a try, but am still getting this error when I use library(rJava):
I am running RStudio1.0.143, OS X El Cap 10.11.6 and Java 8.
Any help would be appreciated, I am out of my league here!
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
In addition: Warning message:
package ‘rJava’ was built under R version 3.4.2
THANK YOU Jack!! This fixed my problem with loading rJava (after countless hours and hours of trying other methods, including other people’s suggestions for making this symlink). Legend! <3
It all seems to be working on OS X 10.11.3 now. I could install the rJava directly using
install.packages("rJava")
but also by installing the R-forge latest version using the R CMD INSTALL method.However I am going nuts trying to figure out what is happening here when I try to use the coreNLP package.
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)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> require(rJava)
Loading required package: rJava
> require(coreNLP)
Loading required package: coreNLP
> initCoreNLP()
Error in rJava::.jnew("edu.stanford.nlp.pipeline.StanfordCoreNLP", basename(path)) :
java.lang.UnsupportedClassVersionError: edu/stanford/nlp/pipeline/StanfordCoreNLP : Unsupported major.minor version 52.0
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.3 (El Capitan)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] coreNLP_0.4-1 rJava_0.9-9
loaded via a namespace (and not attached):
[1] plotrix_3.6-1 XML_3.98-1.3
My Java setup:
KBsMBP15:/ kbenoit$ sudo R CMD javareconf
Password:
Java interpreter : /usr/bin/java
Java version : 1.8.0_74
Java home path : /Library/Java/JavaVirtualMachines/jdk1.8.0_74.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 -I/Library/Java/JavaVirtualMachines/jdk1.8.0_74.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_74.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_74.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_74.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
Done.
Phew that really helped a lot !!! successfully got java 1.8 running with Rstudio was having trouble installing packages like rJava and RWeka otherwise. Thanks a lot !
Pingback: Java 1.8 Setup for R and R Studio | An Active Coder
Thanks!!!!! a lot!!!!!! it works well now!
Hi,
I’m on MacBook Pro. I’m still getting the following errors. Can anybody help me?
> 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
> library(“xlsx”)
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
Error: package ‘rJava’ could not be loaded
No immediate thoughts on the Java side, but I can suggest using the
readxl
package instead ofxlsx
; it doesn’t require Java to run.