Git Product home page Git Product logo

Comments (9)

mpodell avatar mpodell commented on August 28, 2024 1

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`)

from rjava.

gwinstanley avatar gwinstanley commented on August 28, 2024 1

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.

from rjava.

gwinstanley avatar gwinstanley commented on August 28, 2024

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’

from rjava.

gwinstanley avatar gwinstanley commented on August 28, 2024

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'

from rjava.

yasirs avatar yasirs commented on August 28, 2024

@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

from rjava.

gwinstanley avatar gwinstanley commented on August 28, 2024

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`

from rjava.

s-u avatar s-u commented on August 28, 2024

@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

from rjava.

gwinstanley avatar gwinstanley commented on August 28, 2024

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

from rjava.

gwinstanley avatar gwinstanley commented on August 28, 2024

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 :

from rjava.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.