rJava is not picking up the correct Java version - r

I've been struggling with this problem for a while now. When I install rJava from R, it takes Java 1.6 instead of 1.8. I am using MAC OS 10.8.5 and these are the steps that I've already done.
I've installed R.
I have both Java 1.6 and 1.8 on my machine.
I've reconfigures R from terminal using: sudo R CMD javareconf -n.
Here is what the output looks like:
Java interpreter : /usr/bin/java
Java version : 1.8.0_25
Java home path : /Library/Java/JavaVirtualMachines/jdk1.8.0_25.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 progam
detected JNI cpp flags : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
gcc -arch x86_64 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/../include/darwin -I/usr/local/include -fPIC -mtune=core2 -g -O2 -c conftest.c -o conftest.o
gcc -arch x86_64 -std=gnu99 -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 -L/usr/local/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk1.8.0_25.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_25.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
Then I went on to R and installed rJava package as source like this:
install.packages("rJava", type="source")
and I get to DONE(rJava)
then when I check the version using:
library(rJava)
.jinit()
.jcall("java/lang/System", "S", "getProperty", "java.runtime.version")
I still get 1.6.0_25-b17 instead of 1.8
Please let me know if you know the solution to this.

Latest version of rJava has fixed this issue.
I installed latest version from site using command
install.packages("rJava",,"http://rforge.net/",type="source")
Now the output by calling jvm is as:
> library(rJava)
> .jinit()
> .jcall("java/lang/System", "S", "getProperty", "java.runtime.version")
[1] "1.7.0_75-b13"
Note: I have java 1.7 and 1.6 installed, and not java 1.8

What helped me in this case was 2 things (following advice from here: https://www3.ntu.edu.sg/home/ehchua/programming/howto/JDK_Howto.html)
Informing Ubuntu of the java installation that you want to use.
For this I ran:
$ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/java/jdk1.8.0_201/bin/java" 1
$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/jdk1.8.0_201/bin/javac" 1
$ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/java/jdk1.8.0_201/bin/javaws" 1
followed by
$ sudo update-alternatives --set java /usr/java/jdk1.8.0_201/bin/java
$ sudo update-alternatives --set javac /usr/java/jdk1.8.0_201/bin/javac
$ sudo update-alternatives --set javaws /usr/java/jdk1.8.0_201/bin/javaws
Then I could finally see my java in the alternatives in
sudo update-alternatives --config java
where I selected the newly installed java. Then I ran
sudo R CMD javareconf JAVA_HOME=/usr/java/jdk1.8.0_201/bin/

Related

installing data.table on macOS

I need to install data.table 1.12.0 (specifically) on macOS 11.1.
I am getting an error:
clang: error: unsupported option '-fopenmp'
make: *** [assign.o] Error 1
I have followed the directions in https://github.com/Rdatatable/data.table/wiki/Installation#openmp-enabled-compiler-for-mac but still unable to get it to work.
R 3.6.1.
I also tried a R CMD install and got the same error:
R CMD install data.table_1.12.0.tar.gz
* installing to library ‘/Users/XXX/Library/R/3.6/library’
* installing *source* package ‘data.table’ ...
** package ‘data.table’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
clang -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fopenmp -fPIC -Wall -g -O2 -c assign.c -o assign.o
clang: error: unsupported option '-fopenmp'
make: *** [assign.o] Error 1
ERROR: compilation failed for package ‘data.table’
* removing ‘/Users/XXX/Library/R/3.6/library/data.table’
Update: Seems to be an issue install latest data.table from CRAN for me as well, so its not restricted to 1.12.0 apparently.
NB. This solution works with Intel processors (not Apple M1 silicon)
These are the steps I used to install data.table from source with multithreading/openMP enabled (originally described in https://stackoverflow.com/a/65334247/12957340):
Reinstall xcode command line tools (even if it says "up to date")
sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install
Install gcc & llvm via Homebrew (instructions for installing Homebrew) or, if you already have gcc/llvm installed via Homebrew, skip to the next step
# This can take several hours
brew install gcc
brew install llvm
Once you have gcc & llvm installed via Homebrew:
brew cleanup
brew update
brew upgrade
brew reinstall gcc
brew reinstall llvm
Link some headers into /usr/local/include
sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
# You can safely ignore warnings like this:
#ln: /usr/local/include//tcl.h: File exists
#ln: /usr/local/include//tclDecls.h: File exists
#ln: /usr/local/include//tclPlatDecls.h: File exists
#ln: /usr/local/include//tclTomMath.h: File exists
#ln: /usr/local/include//tclTomMathDecls.h: File exists
#ln: /usr/local/include//tk.h: File exists
#ln: /usr/local/include//tkDecls.h: File exists
#ln: /usr/local/include//tkPlatDecls.h: File exists
Install the gmp library:
brew install gmp
Create a new ~/.R/Makevars file (if you already have a ~/.R/Makevars file, save it in a different directory (away from ~/.R/)) and include only these lines in the file:
LOC=/usr/local/gfortran
CC=$(LOC)/bin/gcc -fopenmp
CXX=$(LOC)/bin/g++ -fopenmp
CXX11 = $(LOC)/bin/g++ -fopenmp
CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS=-L$(LOC)/lib -Wl,-rpath,$(LOC)/lib,-L/usr/local/lib
CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/usr/local/include
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/10.2.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
CXX1X=/usr/local/gfortran/bin/g++
CXX98=/usr/local/gfortran/bin/g++
CXX11=/usr/local/gfortran/bin/g++
CXX14=/usr/local/gfortran/bin/g++
CXX17=/usr/local/gfortran/bin/g++
Compile data.table from source in R/Rstudio
install.packages("data.table", type = "source")
# To check whether it installed correctly, run:
library(data.table)
If your package fails to compile, a couple of SO users have had to install a fresh gfortran (re: https://stackoverflow.com/a/65334247/12957340), which you can download from https://github.com/fxcoudert/gfortran-for-macOS/releases
here has been my approach
basically you have to download clang and llvmorg
curl -SL https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.0/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz \
| tar -xJC . && \
mv clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04 clang_10.0.0 && \
echo 'export PATH=/clang_10.0.0/bin:$PATH' >> ~/.bashrc && \
echo 'export LD_LIBRARY_PATH=/clang_10.0.0/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
and make sure they are listed in you PATH and LD_LIBRARY_PATH
then get the datatable and build from the source
git clone https://github.com/h2oai/datatable.git && \
cd datatable && \
make build && \
python -m pip install ../datatable/
make sure you are using the right Python

rJava package installation on Mac 10.14

I’m running into issues while trying to install the rJava package on my Mac. I have tried the solutions proposed online, including running R CMD javareconf, but without success (output is below). Any advice on how to fix this issue would be much appreciated.
Among the things I tried is the solution proposed here: rJava installation fails on macOS 10.14
open macOS_SDK_headers_for_macOS_10.14.pkg```
This had worked for me with my previous Mac, but not this time.
Output of:
```sudo R CMD javareconf```
Java interpreter : /usr/bin/java
Java version : 12.0.2
Java home path : /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
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
/usr/local/Cellar/gcc/9.1.0/bin/gcc-9 -I"/usr/local/Cellar/r/3.6.1/lib/R/include" -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home/include/darwin -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include -fPIC -g -O2 -c conftest.c -o conftest.o
/usr/local/Cellar/gcc/9.1.0/bin/gcc-9 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/Cellar/r/3.6.1/lib/R/lib -L/usr/local/Cellar/gcc/9.1.0/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home/lib/server -ljvm -L/usr/local/Cellar/r/3.6.1/lib/R/lib -lR -lintl -Wl,-framework -Wl,CoreFoundation
ld: library not found for -lintl
collect2: error: ld returned 1 exit status
make: *** [conftest.so] Error 1
Unable to compile a JNI program
JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
Java library path:
JNI cpp flags :
JNI linker flags :
Updating Java configuration in /usr/local/Cellar/r/3.6.1/lib/R
Done.

Docker, Jenkins, and rJava

Part of a Jenkins job I'm running entails installing rJava; the Jenkins job is running on a docker image over which I have control, but still I can't seem to get rJava to install.
Looking around a bit, it seems like being sure to run R CMD javareconf is pretty key in all this, and indeed I added && R CMD javareconf as the last command in my Dockerfile. Compilation of the Docker image contains:
Java interpreter : /docker-java-home/jre/bin/java
Java version : 1.8.0_171
Java home path : /docker-java-home
Java compiler : /docker-java-home/bin/javac
Java headers gen.: /docker-java-home/bin/javah
Java archive tool: /docker-java-home/bin/jar
trying to compile and link a JNI program
detected JNI cpp flags : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
detected JNI linker flags : -L$(JAVA_HOME)/jre/lib/amd64/server -ljvm
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/docker-java-home/include -I/docker-java-home/include/linux -fpic -g -O2 -fdebug-prefix-map=/build/r-base-3.3.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c conftest.c -o conftest.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-z,relro -o conftest.so conftest.o -L/docker-java-home/jre/lib/amd64/server -ljvm -L/usr/lib/R/lib -lR
JAVA_HOME : /docker-java-home
Java library path: $(JAVA_HOME)/jre/lib/amd64/server
JNI cpp flags : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
JNI linker flags : -L$(JAVA_HOME)/jre/lib/amd64/server -ljvm
Updating Java configuration in /usr/lib/R
Done.
Now, when I enter the Docker instance (docker exec -it jenkins_img bash), I can in fact manually install rJava: install.packages('rJava') works as expected; importantly, this is part of the output there:
checking Java support in R... present:
interpreter : '/docker-java-home/jre/bin/java'
archiver : '/docker-java-home/bin/jar'
compiler : '/docker-java-home/bin/javac'
header prep.: '/docker-java-home/bin/javah'
cpp flags : '-I/docker-java-home/include -I/docker-java-home/include/linux'
java libs : '-L/docker-java-home/jre/lib/amd64/server -ljvm'
However, when I run the script that's part of my Jenkins job, the corresponding output becomes:
checking Java support in R... present:
interpreter : '/usr/lib/jvm/default-java/jre/bin/java'
archiver : '/usr/lib/jvm/default-java/bin/jar'
compiler : '/usr/lib/jvm/default-java/bin/javac'
header prep.: '/usr/lib/jvm/default-java/bin/javah'
cpp flags : '-I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux'
java libs : '-L/usr/lib/jvm/default-java/jre/lib/amd64/server -ljvm'
checking whether Java run-time works... ./configure: line 3747: /usr/lib/jvm/default-java/jre/bin/java: No such file or directory
no
configure: error: Java interpreter '/usr/lib/jvm/default-java/jre/bin/java' does not work
So, it seems whichever process is running when I docker exec to bash has a different JAVA_HOME than the process which is running under my Jenkins job. To that end, I added the following directly before the line installing rJava in my script (as suggested):
export JAVA_HOME=/docker-java-home/
export PATH=$PATH:$JAVA_HOME/bin
Rscript -e 'install.packages("rJava")'
However, this fails with the same output. I have also checked that the R process running install.packages has the same JAVA_HOME by cating Sys.getenv('JAVA_HOME') and confirming that it's /docker-java-home/.
The Jenkins script doesn't have root access so I can't run R CMD javareconf there.
How can I get install.packages('rJava') to use the right JAVA_HOME?
FWIW, these days I use some variation of the following in Docker images to get rJava to build correctly:
# rJava
RUN apt-get update && apt-get install -y libudunits2-dev gnupg2 software-properties-common
RUN echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" \
| tee /etc/apt/sources.list.d/webupd8team-java.list \
&& echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" \
| tee -a /etc/apt/sources.list.d/webupd8team-java.list \
&& apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886 \
&& echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" \
| /usr/bin/debconf-set-selections \
&& apt-get update \
&& apt-get install -y oracle-java8-installer \
&& update-alternatives --display java \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean \
&& R CMD javareconf
This was not particularly my own work, I basically took it from #daroczig:
https://github.com/rocker-org/rocker/issues/109
https://hub.docker.com/r/cardcorp/r-java/dockerfile/

OS X clang rpy2

I'm trying to install the python library rpy2 in OS X (El Capitan) with R 3.4.
I did follow many posts on web and I end up replacing clang from xcode to one manually installed (after following http://thecoatlessprofessor.com/programming/openmp-in-r-on-os-x/#clang-before-3-4-0):
clang version 4.0.0 (tags/RELEASE_400/final)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /usr/bin
Still, when I tried to install rpy2 (sudo pip3 install rpy2):
clang -bundle -undefined dynamic_lookup build/temp.macosx-10.11-x86_64-3.6/./rpy/rinterface/_rinterface.o -L/usr/local/lib -L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/sqlite/lib -Lbuild/temp.macosx-10.11-x86_64-3.6 -L/usr/local/lib -lpcre -llzma -lbz2 -lz -licucore -lm -liconv -lr_utils -o build/lib.macosx-10.11-x86_64-3.6/rpy2/rinterface/_rinterface.cpython-36m-darwin.so -fopenmp -F/Library/Frameworks/R.framework/.. -framework R
ld: library not found for -lomp
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'clang' failed with exit status 1
What's lomp? I'm not sure what I can do now.
UPDATE:
I try to reinstall llvm through brew:
brew reinstall --with-clang --with-lld --with-python --HEAD llvm
now installing rpy2 I got:
clang -bundle -undefined dynamic_lookup build/temp.macosx-10.11-x86_64-3.6/./rpy/rinterface/_rinterface.o -L/usr/local/lib -L/usr/local/lib -L/usr/local/opt/openssl/lib -L/usr/local/opt/sqlite/lib -Lbuild/temp.macosx-10.11-x86_64-3.6 -L/usr/local/lib -lpcre -llzma -lbz2 -lz -licucore -lm -liconv -lr_utils -o build/lib.macosx-10.11-x86_64-3.6/rpy2/rinterface/_rinterface.cpython-36m-darwin.so -fopenmp -F/Library/Frameworks/R.framework/.. -framework R
ld: library not found for -lomp
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'clang' failed with exit status 1
Although from the llvm installation I got the advice:
For compilers to find this software you may need to set:
LDFLAGS: -L/usr/local/opt/llvm/lib
CPPFLAGS: -I/usr/local/opt/llvm/include
do you know how to set it? It may be the reason it fails
This was discussed on the rpy2 issue tracker: https://bitbucket.org/rpy2/rpy2/issues/403/cannot-pip-install-rpy2-with-latest-r-340
Note that the issue was resolved and a precompiled binary wheel for rpy2-2.9.3 is now available on pypi.

Unable to install rJava package in RedHat 5 (R 3.3.0) | Unable to compile a JNI Program

when I am running install.packages("rJava")
Error:-
checking whether siglongjmp is declared... yes
checking Java support in R... present:
interpreter : '/usr/bin/java'
archiver : '/usr/bin/jar'
compiler : '/usr/bin/javac'
header prep.: '/usr/bin/javah'
cpp flags : ''
java libs : ''
configure: error: One or more Java configuration variables are not set.
Make sure R is configured with full Java support (including JDK). Run
R CMD javareconf
as root to add Java support to R.
If you don't have root privileges, run
R CMD javareconf -e
to set all Java-related variables and then install rJava.
ERROR: configuration failed for package ‘rJava’
* removing ‘/usr/lib64/R/library/rJava’
The downloaded source packages are in
‘/tmp/Rtmpyht6l8/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done
Warning message:
In install.packages("rJava", "/usr/lib64/R/library", repos = "http://ftp.iitm.ac .in/cran/") :
installation of package ‘rJava’ had non-zero exit status
I ran R CMD javareconf -e
Java interpreter : /usr/bin/java
Java version : 1.8.0_91
Java home path : /usr/java/jdk1.8.0_91/jre
Java compiler : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
trying to compile and link a JNI program
detected JNI cpp flags : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/linux
detected JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG -I/usr/java/jdk1.8.0_91/jre/../include -I/usr/java/jdk1.8.0_91/jre/../include/linux -I/usr/local/include -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fpic -fPIC -c conftest.c -o conftest.o
gcc -m64 -std=gnu99 -shared -L/usr/lib64/R/lib -ldl -lpthread -lc -lrt -lcurl -lidn -lssh2 -lssl -lcrypto -lssl -lcrypto -ldl -lz -lgssapi -lldap -lz -lrt -o conftest.so conftest.o -L/usr/java/jdk1.8.0_91/jre/lib/amd64/server -ljvm -L/usr/lib64/R/lib -lR
/usr/bin/ld: cannot find -lcurl
collect2: ld returned 1 exit status
make: *** [conftest.so] Error 1
Unable to compile a JNI program
The following Java variables have been exported:
JAVA_HOME JAVA JAVAC JAVAH JAR JAVA_LIBS JAVA_CPPFLAGS JAVA_LD_LIBRARY_PATH
Also export JAVA_HOME= jdk_path
Can anyone please help me in this?
Install latest java version in your work station and set java home page inside Rstudio, i think it may work
Sys.setenv(JAVA_HOME="C:\\Program Files\\Java\\jre7")

Resources