15

I have the very common problem that rJava does not install correctly on Ubuntu. This problem has been dsicussed in multiple places here, here, here, to name a few.

The basic problem is that on installing the rJava package, the following error message is produced

configure: error: Unable to run a simple JNI program. Make sure you have configured R with Java support (see R documentation) and check config.log for failure reason. Warning in system(cmd) : error in running command ERROR: configuration failed for package ‘rJava’ * removing ‘/home/jonno/R/x86_64-pc-linux-gnu-library/3.6/rJava’ 

There are various closely related solutions to this problem. Most of them use sudo R CMD javareconf to configure Java for R (also a -e variant). Some suggest setting the JAVA_HOME path in the environment variables (others say not to). Others suggest uninstalling and re-installing R whilst others suggest installing rJava from cran. There are several who reccomend update alternatives. There are other variants of these solutions.

I have tried combinations of all of the above, and have got nowhere, so am clearly doing something wrong.

entering echo $JAVA_HOME returns

/usr/lib/jvm/java-11-openjdk-amd64

my etc/environment looks like this

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/$ MKL_THREADING_LAYER=GNU JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64" 

When I run R CMD javaconf, it looks like this

Java interpreter : /usr/lib/jvm/java-11-openjdk-amd64/java Java version : 11.0.4 Java home path : /usr/lib/jvm/java-11-openjdk-amd64 Java compiler : /usr/lib/jvm/java-11-openjdk-amd64/bin/javac Java headers gen.: /usr/bin/javah Java archive tool: /usr/lib/jvm/java-11-openjdk-amd64/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/server -ljvm gcc -std=gnu99 -I"/usr/share/R/include" -DNDEBUG -I/usr/lib/jvm/java-11-openjdk-amd64/include -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux -fpic -g -O2 -fdebug-prefix-map=/build/r-base-uuRxut/r-base-3.6.1=. -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,-Bsymbolic-functions -Wl,-z,relro -o conftest.so conftest.o -L/usr/lib/jvm/java-11-openjdk-amd64/lib/server -ljvm -L/usr/lib/R/lib -lR JAVA_HOME : /usr/lib/jvm/java-11-openjdk-amd64 Java library path: $(JAVA_HOME)/lib/server JNI cpp flags : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm Updating Java configuration in /usr/lib/R Done. 

What am I doing wrong and how do I get rJava to install properly?

EDIT:

having managed to successfully install rJava using sudo apt-get install r-cran-rjava I know get the following error

Error: package or namespace load failed for ‘rJava’: .onLoad failed in loadNamespace() for 'rJava', details: call: dyn.load(file, DLLpath = DLLpath, ...) error: unable to load shared object '/usr/lib/R/site-library/rJava/libs/rJava.so': libjvm.so: cannot open shared object file: No such file or directory 
1
  • Did you manage to install sudo apt-get install r-cran-rjava for R 3.6? If so, how? The only support for this way of installing rJava I have found is for R 3.4 and R 3.5. Commented Apr 17, 2020 at 11:11

1 Answer 1

23

I've investigated with the original poster (we work at the same place) and the problem is that in OpenJDK11 they moved around some of the .so files that the JVM lives in, specifically libjvm.so which in the Ubuntu package is now in /usr/lib/jvm/java-1.11.0-openjdk-amd64/lib/server/.

This means that even if you install the Ubuntu package for rJava with apt install r-cran-rjava it fails when you try to library(rJava).

The solution is to add /usr/lib/jvm/java-1.11.0-openjdk-amd64/lib/server/ to your $LD_LIBRARY_PATH by adding:

export LD_LIBRARY_PATH=/usr/lib/jvm/java-1.11.0-openjdk-amd64/lib/server:$LD_LIBRARY_PATH 

to the end of your ~/.bashrc and starting a new shell (or source ~/.bashrc).

This is something we had to fix for our central installs of OpenJDK e.g. here: https://github.com/UCL-RITS/rcps-buildscripts/blob/master/adoptopenjdk-11.0.3_install.sh#L46

If you want to make this work with Rstudio launched from Gnome, you need to add that directory to ldconfig.

As root (or with sudo) create a file in /etc/ld.so.conf.d/ which you should call something with a .conf extension e.g. java.conf which contains the line:

/usr/lib/jvm/java-1.11.0-openjdk-amd64/lib/server 

And then as root run

ldconfig -v 

This should add the directory to the locations that executables launched through GNOME search for. This particular part of the problem (GNOME ignoring settings in bashrc) has been a problem in Ubuntu since at least 9.04 (https://bugs.launchpad.net/ubuntu/+source/xorg/+bug/366728/).

Sign up to request clarification or add additional context in comments.

4 Comments

The first half of the answer means that if you launch rstudio from the terminal rJava will work, but if you double click the Rstudio icon rJava won't work. The second part of the answer means that you can launch Rstudio the normal way (using the icon) and rJava will work. The first half is easier to do, but the second half means you don't need to change you behaviour when launching rstudio.
I'd just like to add what I did for future reference. Since my Java installation is different, the correct thing to do is first find libjvm.so: find /usr/lib/jvm/<java-dir>/**/libjvm.so, then add that folder (in my case jre/lib/amd64/server) to LD_LIBRARY_PATH as per this answer. (Just wanted to point that out so it's easier to follow for other people).
The path specifies the java version (eg. java-1.11.0-openjdk-amd64 above), which means it will probably break when the java package is updated. Is there a better way to do this, without hard-coding a specific version's path, so that it survives updates?
I ran into the exact same problem and the solution posted by @owainkenway is what worked. In particular, the second part of the solution I found is critical to ensure I can install "rJava" correctly. I can now install any Java based packages via both R and RStudio

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.