Chris Carter
Chris Carter

Reputation: 41

rJAVA wrong java version

This problem has plagued me for a week now. I have installed rJAVA from R running on a Ubuntu server 14.04. I installed rJava package as source like this:

install.packages("rJava", type="source")

and I get:

DONE(rJava)

I have installed java-8-openjdk and when running java -version I get the correct version and this is the output:

openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-0ubuntu4~14.04-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

However when I check the java version in R using either

sudo R CMD javareconf 

or

library(rJava)
jinit()
.jcall("java/lang/System", "S", "getProperty", "java.runtime.version") 

The java version is still set to 1.7:

Java interpreter : /usr/lib/jvm/default-java/jre/bin/java
Java version     : 1.7.0_101
Java home path   : /usr/lib/jvm/default-java
Java compiler    : /usr/lib/jvm/default-java/bin/javac
Java headers gen.: /usr/lib/jvm/default-java/bin/javah
Java archive tool: /usr/lib/jvm/default-java/bin/jar

trying to compile and link a JNI program 
detected JNI cpp flags    : -I$(JAVA_HOME)/include
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 -I/usr/lib/jvm/default-java/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/default-java
Java library path: /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server
JNI cpp flags    : -I$(JAVA_HOME)/include
JNI linker flags : -L/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server -ljvm
Updating Java configuration in /usr/lib/R
Done.

Please let me know what I'm missing.

Upvotes: 4

Views: 3518

Answers (3)

jburkhardt
jburkhardt

Reputation: 675

I had the same issue with rJava on Ububtu 14.04. Changing the first line in file /usr/lib/R/etc/javaconf to : ${JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre/bin/java} and running sudo R CMD javareconfsolved the issue in my case.

Upvotes: 2

Peter P
Peter P

Reputation: 128

I had the same problem: java -version gave Java 8, but sudo R CMD javareconf gave Java 7.

Running sudo R CMD javareconf --help gives:

Environment variables that can be used to influence the detection:
JAVA           path to a Java interpreter executable
               By default first 'java' command found on the PATH
               is taken (unless JAVA_HOME is also specified).
JAVA_HOME      home of the Java environment. If not specified,
               it will be detected automatically from the Java
               interpreter.

The documentation tells us to do what Dirk suggested: ensure that the first java found on PATH is the one we want. That did not help in my case, even though JAVA_HOME was not set. Abdou's suggestion of setting JAVA_HOME in my .Rprofile dit not help either.

To solve the problem, I had to set root's JAVA_HOME to the one I wanted (because R CMD javareconf is run as sudo):

sudo -i
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))
R CMD javareconf
exit

Upvotes: 3

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

I think 'all' you need to do is to

  1. ensure your 'new' Java comes first in the $PATH; its installer may have appended to the end -- so correct that.

  2. ensure you run sudo R CMD javareconf with that path.

Taking these two together maybe

PATH=/opt/java/whatever/bin:$PATH sudo R CMD javareconf

is all it takes. Adjust the path on that like as needed.

Lastly, rJava from source may not be needed. I do sudo apt-get install r-cran-rjava.

Upvotes: 1

Related Questions