Installation of rJava Installation of rJava r r

Installation of rJava


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 javareconfJava interpreter : /usr/bin/javaJava version     : 1.7.0_91Java home path   : /usr/lib/jvm/java-7-openjdk-amd64/jreJava compiler    : /usr/bin/javacJava headers gen.: /usr/bin/javahJava archive tool: /usr/bin/jartrying to compile and link a JNI program detected JNI cpp flags    : -I$(JAVA_HOME)/../includedetected JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvmgcc -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.ogcc -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 -lRJAVA_HOME        : /usr/lib/jvm/java-7-openjdk-amd64/jreJava library path: $(JAVA_HOME)/lib/amd64/serverJNI cpp flags    : -I$(JAVA_HOME)/../includeJNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm


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.


First i would recommend installing Rstudio from its website: https://www.rstudio.com/products/rstudio/download/ (i.e. Rstudio 64bit: https://download1.rstudio.org/rstudio-0.99.489-amd64.deb). This does not solve the problem directly, but it helps to avoid other bugs with Rstudio.

Regarding the error, trying to make sure you have JDK install. I don't think the command java -version can tell if JDK is installed. You have to check the package of JDK itself, or based on the error message, do this:

locate jni.hThe output should match or compatible with your JAVAHOME, e.g:

/usr/lib/jvm/java-7-openjdk-amd64/include/jni.h /usr/lib/jvm/java-7-oracle/include/jni.h        

Update 1:R CMD javareconf is looking for the jni.h file under $(JAVA_HOME)/includeYou have JDK installed, but it is very likely that you are having default java to a JRE directory, that why the error happened.

You can see where default-java is really pointing to by doing this command:

jRealDir=$(readlink -f /usr/lib/jvm/default-java)echo $jRealDir# sample correct output: /usr/lib/jvm/jdk1.8.0_65 # or /usr/lib/jvm/java-8-oracle if you default to Oracle's# now check jni.hls -l $jRealDir/include/jni.h# sample expected output:# /usr/lib/jvm/jdk1.8.0_65/include/jni.h

If the ls command failed, you have to setup so that javareconf ( and later rJava) can use the java from JDK not from JRE. You have two options:

Method 1: Do it system-wide

This is convenient, but may effect other program like the one you mentioned jmol. But don't worry, this is revertible, just re-run the command and pick the old one. Do the following command and pick the dir that has JDK:

sudo update-alternatives --config java

After that test how jmol works, if it works alright then congrat. You are now ready to test rJava. If not, try the second method below

Method 2: Do it for R only

put this in the .Rprofile under your home directory

Sys.setenv(JAVA_HOME = '/usr/lib/jvm/jdk1.8.0_65')# this set JAVA_HOME for R to correct java home dir. 

After updating or creating the .Rprofile DO restart R in Rstudio. The R CMD javareconf may still fail in this case, but it should be OK if you run it from Shell under Tools menu of Rstudio.

Regarding the installing or Rstudio from Ubuntu's stock repo. It would not make a difference for getting rJava running. Then again, I recommend installing Rstudio for its homepage because new version also has some nice features (i.e. better autocompletion, which I like the most).