Comments (9)
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.
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.
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.
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.
@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.
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.
@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.
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.
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)
- avoid calling Sys.getlocale("LC_MESSAGES") on Windows HOT 5
- should rJava use the same version of Java that R was configured with by default? HOT 2
- Infinite Recursion in RinitJVM_with_padding HOT 6
- rJava install problems for java 19.0.2 2023-01-17
- rJava install problems for java 19.0.2 2023-01-17 HOT 5
- Upgrade bundled dependencies with recent security patches - jszip HOT 4
- Typo in file -- configure.win-- INGORE -- you probably meant IGNORE HOT 2
- wrong class in method signatures HOT 5
- R freezes when executing rJava::.jinit() HOT 1
- how to load jar by J()? HOT 1
- Termination Error with rJava When Executing Complex R Code from Java Application HOT 3
- rJava issue HOT 1
- Struggling with rJava and dependent packages in Rstudio Mac M1. Error: package or namespace load failed for ‘rJava’: HOT 3
- Segmentation Fault when try re.eval("library('forecast')") HOT 2
- *** caught segfault *** address 0xfffffffffffffff8, cause 'invalid permissions' HOT 1
- long vectors not supported yet: memory.c:3888 HOT 1
- Please submit rJava 1.0-8 to CRAN HOT 1
- Error: "Sorry, parameter type 'X' is ambiguous or not supported." HOT 2
- Error: package or namespace load failed for ‘rJava’ HOT 2
- Can't install from source package on Mac OS 14.2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rjava.