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

Cannot install on Yosemite: homebrew-cask java 1.8.0_25, homebrew R 3.1.2 #36

Closed
mpodell opened this issue Nov 14, 2014 · 9 comments
Closed

Comments

@mpodell
Copy link

mpodell commented Nov 14, 2014

Not sure this is a rJava problem but looking for help.

Installing R via homebrew gives me this message:

To enable rJava support, run the following command:
  R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers
If you've installed a version of Java other than the default, you might need to instead use:
  R CMD javareconf JAVA_CPPFLAGS='-I/System/Library/Frameworks/JavaVM.framework/Headers -I/Library/Java/JavaVirtualMachines/jdk<version>.jdk/'
  (where <version> can be found by running `java -version` or `locate jni.h`)

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

running either (subbing jdk1.8.0_25.jdk for <version>) fails not finding jni.h

java installs jni.h into

/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/include

Since there was nothing at /System/Library/Frameworks/JavaVM.framework/Headers/ where javareconf seems to want to look for it, I created symlinks:

cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo ln -nsf /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents ./CurrentJDK
cd /System/Library/Frameworks/JavaVM.framework
sudo ln -nsf Versions/CurrentJDK/Home/include/ ./Headers

and added

export JAVA_HOME = "/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home"

to ~/.bash_profile since without it javareconf says the JAVA_HOME is not a valid path. This allows javareconf to find jni.h using:

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

but then javareconf fails because it can't find jni_md.h which is in /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/include/dawrin

looking at javareconf there is this code:

    ## only if we get jni.h we can try to find jni_md.h
    if test -n "${jinc}"; then
       JAVA_CPPFLAGS="-I${jinc}"
       jmdinc=''
       jmdirs=''
   ## we are not in configure, so we need to find the OS from R
   host_os=`echo 'cat(R.version$os)'|${R_HOME}/bin/R --vanilla --slave 2>/dev/null`
       ## put the most probable locations for each system in the first place
       case "${host_os}" in
         darwin*)  jmdirs=darwin;;
         linux*)   jmdirs=linux;;
         bsdi*)    jmdirs=bsdos;;
         osf*)     jmdirs=alpha;;
         solaris*) jmdirs=solaris;;
         freebsd*) jmdirs=freebsd; add_java_libs='-lpthread';;
       esac

I would think that this would allow javareconf to find the jni_md.h but that's not happening.

This may not be a rJava issue but since the R install said to run javareconf in order to enable rJava support, I thought I would post here, but then again, I know just enough to know that I know nothing about this stuff. I'm just bummed I can't get rJava installed in R.

Thanks for taking the time to read this.

@mpodell
Copy link
Author

mpodell commented Nov 15, 2014

Update:

I can get rJava support complied if I pass the actual location of the jni.h file (duh):

JAVA_CPPFLAGS=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/include`

to R CMD javareconf

seems that for Yosemite the help message for R should be modified as the current message below is inaccurate.

To enable rJava support, run the following command:
  R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers
If you've installed a version of Java other than the default, you might need to instead use:
  R CMD javareconf JAVA_CPPFLAGS='-I/System/Library/Frameworks/JavaVM.framework/Headers -I/Library/Java/JavaVirtualMachines/jdk<version>.jdk/'
  (where <version> can be found by running `java -version` or `locate jni.h`)

@gwinstanley
Copy link

This still doesn't resolve the problem, as when the script detects what it assumes to be "system Java", it overrides the JAVA_CPPFLAGS to be -I/System/Library/Frameworks/JavaVM.framework/Headers instead of the custom value supplied. Looking at the script, the correct $JAVA_HOME/include folder should be detected ok once the non-system Java is detected correctly, but it would also need the $JAVA_HOME/include/darwin folder to be included for correct operation.

Unfortunately installing the Apple Java for OS X 2014-001 package doesn't work either, which I hoped would should provides the necessary headers in the expected locations.

The error remains the same, and it's clear the JAVA_CPPFLAGS are not being passed through correctly:

clang -I/usr/local/Cellar/r/3.1.2/R.framework/Resources/include -DNDEBUG -I/System/Library/Frameworks/JavaVM.framework/Headers -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include    -fPIC  -g -O2  -c conftest.c -o conftest.o
conftest.c:1:10: fatal error: 'jni.h' file not found
#include <jni.h>
         ^
1 error generated.

The simplest way to get it working seems to be to modify the script by preventing it detecting the host OS. For example, I simply modified the line if test "${hostos}" = "Darwin"; then to be ``if test "${hostos}" = "DarwinX"; then` and the script ran fine. However, I'm still experiencing issues with loading the rJava package within R, as follows:

> library(rJava)
Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/usr/local/Cellar/r/3.1.2/R.framework/Versions/3.1/Resources/library/rJava/libs/rJava.so':
  dlopen(/usr/local/Cellar/r/3.1.2/R.framework/Versions/3.1/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
  Referenced from: /usr/local/Cellar/r/3.1.2/R.framework/Versions/3.1/Resources/library/rJava/libs/rJava.so
  Reason: image not found
Error: package or namespace load failed for ‘rJava’

@gwinstanley
Copy link

Ok, turns out for my case it was loading rJava correctly with the terminal version of R, but not in RStudio. For some reason the LD_LIBRARY_PATH configuration in RStudio was omitting the required JRE folder ($JAVA_HOME/jre/lib/server). I can now successfully load rJava in RStudio by launching it from the terminal after defining a suitable alias in my .bashrc:

alias rstudio='LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/server: open -a RStudio'

@yasirs
Copy link

yasirs commented Jul 15, 2015

@gwinstanley
What is $JAVA_HOME in OSX?
I get $ which java
/usr/bin/java
ls -alth /usr/bin/java
lrwxr-xr-x 1 root wheel 74B Oct 23 2013 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

@gwinstanley
Copy link

Good point @yasirs, I forgot to mention that I usually set the JAVA_HOME variable in my .zshenv/.bashrc file. This is the line I usually include in mine:

export JAVA_HOME=`/usr/libexec/java_home`

or if you want a specific version of Java (e.g. Java 8):

export JAVA_HOME=`/usr/libexec/java_home -v1.8`

@s-u
Copy link
Owner

s-u commented Jul 16, 2015

@gwinstanley check with RStudio - one way to make sure that you have correct vars set is to use R CMD open -a RStudio or similar. Normally, RStudio should be using R's vars, but I'm not sure if they actually do. Either way, always test with R first to make sure it's not a bug in RStudio (which is not R).
Also do not set JAVA_HOME

As for configuration, with properly installed Oracle Java (do not set JAVA_HOME manually!) you should see something like

$ R CMD javareconf
Java interpreter : /usr/bin/java
Java version     : 1.8.0_20
Java home path   : /Library/Java/JavaVirtualMachines/jdk1.8.0_20.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_20.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_20.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_20.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_20.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

@gwinstanley
Copy link

I like the idea of launching from the R CMD ..., which should pick up the Java config from the R installation. Regarding JAVA_HOME though, I have three Java installations on my machine, so I have it set to configure many other items from the specific Java version I'm using. I found the standard javareconf script failed without both disabling the OS auto-detect for Darwin, and custom configuring JAVA_HOME & JAVA_CPPFLAGS. It would make sense if javareconf correctly detected this (e.g. using jdk1.8.0_xx), but it didn't work for me, hence the workaround I posted. That said, perhaps it's updated since I last tried (27 Nov 2014).

@gwinstanley
Copy link

So I've just upgraded to the latest Hombrew version of R (3.2.1_1) and latest Java (1.8.0_51), and I get the following using the recommended
R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers:
However, it works fine using my technique as shown above.

Java interpreter : /usr/bin/java
Java version     : 1.8.0_51
Java home path   : /Library/Java/JavaVirtualMachines/jdk1.8.0_51.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/System/Library/Frameworks/JavaVM.framework/Headers
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
clang -I/usr/local/Cellar/r/3.2.1_1/R.framework/Resources/include -DNDEBUG -I/System/Library/Frameworks/JavaVM.framework/Headers -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include    -fPIC  -g -O2  -c conftest.c -o conftest.o
conftest.c:1:10: fatal error: 'jni.h' file not found
#include <jni.h>
         ^
1 error generated.
make: *** [conftest.o] Error 1
Unable to compile a JNI program


JAVA_HOME        : /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre
Java library path:
JNI cpp flags    :
JNI linker flags :

@s-u s-u closed this as completed Jan 29, 2016
@gwinstanley
Copy link

BTW, if anyone's arrived at this thread still looking for a fix for the latest Java/rJava, I've created a gist for it.

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

No branches or pull requests

4 participants