Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installation of rJava

Tags:

I've tried to solve this using the previous questions/answers on SO but without any success. So, here's my problem.

I'm using RStudio on and Ubuntu box (14.04) and I tried to upgrade rJava from sources and in the process I managed to lose it.

I tried to install it again using,

install.packages("rJava") 

which returned the following error message,

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 ‘/home/darren/R/x86_64-pc-linux-gnu-library/3.2/rJava’ Warning in install.packages :   installation of package ‘rJava’ had non-zero exit status 

So, I went to the terminal and typed,

sudo R CMD javareconf 

which also gave the following error,

trying to compile and link a JNI program  detected JNI cpp flags    :  detected JNI linker flags : -L/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server -ljvm gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG      -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c conftest.c -o conftest.o conftest.c:1:17: fatal error: jni.h: No such file or directory  #include <jni.h>                  ^ compilation terminated. make: *** [conftest.o] Error 1 Unable to compile a JNI program   JAVA_HOME        : /usr/lib/jvm/default-java Java library path:  JNI cpp flags    :  JNI linker flags :  Updating Java configuration in /usr/lib/R Done. 

I tried to follow these links, one and two but they didn't seem to resolve my issue; there are more links on SO but I'm not sure which one to follow. I've also un-installed and re-installed RStudio via the Ubuntu Software Centre but this didn't make any difference.

Can anyone else help?

In short, I want to be able to use RStudio with rJava again without it destroying any other uses of Java (such as jmol).

like image 548
DarrenRhodes Avatar asked Dec 10 '15 21:12

DarrenRhodes


People also ask

Do you need Java for rJava?

Prerequisites. The rJava package requires the following. A bit-matching 32-bit or 64-bit version of the Java Runtime Environment (JRE), version 6 or later is installed. The system variable JAVA_HOME is set.

What is rJava package?

rJava provides a low-level bridge between R and Java (via JNI). It allows to create objects, call methods and access fields of Java objects from R. rJava release versions can be obtained from CRAN - usually install. packages("rJava") in R will do the trick.

How do I add Java support to R?

Make sure R is configured with full Java support (including JDK). Run R CMD javareconf as root to add Java support to R. Running sudo R CMD javareconf , I can see that Java headers gen.: is not set, but I have failed to figure out how to set it. I do have javah in /usr/bin .


Video Answer


2 Answers

You don't seem to have JDK installed. You will need at least

sudo apt-get install openjdk-7-jdk 

then re-run

sudo R CMD javareconf 

Make sure you do NOT set JAVA_HOME by hand - it will be detected automatically. You should then see something like this:

$ sudo R CMD javareconf Java interpreter : /usr/bin/java Java version     : 1.7.0_91 Java home path   : /usr/lib/jvm/java-7-openjdk-amd64/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 detected JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/lib/jvm/java-7-openjdk-amd64/jre/../include     -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c conftest.c -o conftest.o gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o conftest.so conftest.o -L/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server -ljvm -L/usr/lib/R/lib -lR   JAVA_HOME        : /usr/lib/jvm/java-7-openjdk-amd64/jre Java library path: $(JAVA_HOME)/lib/amd64/server JNI cpp flags    : -I$(JAVA_HOME)/../include JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm 
like image 136
Simon Urbanek Avatar answered Dec 09 '22 14:12

Simon Urbanek


What is wrong with sudo apt-get install r-cran-rjava ?

See for example this earlier answer and the question / thread around it.

For an installation from scratch, you could still much worse than starting from sudo apt-get build-dep r-cran-rjava. It will get you the JDK corresponding to your Ubuntu version.

like image 45
Dirk Eddelbuettel Avatar answered Dec 09 '22 14:12

Dirk Eddelbuettel