I try to install rJava package following "sudo R CMD javareconf" and in R "install.packages('rJava', type = "source"), but it does not work.

First, I installed my Java in this way

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

In my .bashrc, I have been setting

export JAVA_HOME=/usr/lib/jvm/java-8-oracle
export PATH=$PATH:$JAVA_HOME/bin


     sudo R CMD javareconf 
*** JAVA_HOME is not a valid path, ignoring
Java interpreter : /usr/bin/java
Java version     : 1.8.0_111
Java home path   : /usr/lib/jvm/java-8-oracle/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 -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/lib/jvm/java-8-oracle/jre/../include -I/usr/lib/jvm/java-8-oracle/jre/../include/linux     -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-8-oracle/jre/lib/amd64/server -ljvm -L/usr/lib/R/lib -lR

JAVA_HOME        : /usr/lib/jvm/java-8-oracle/jre
Java library path: $(JAVA_HOME)/lib/amd64/server
JNI cpp flags    : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/linux
JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
Updating Java configuration in /usr/lib/R


    Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/home/sunxd/R/x86_64-pc-linux-gnu-library/3.3/rJava/libs/rJava.so':
  libjvm.so: cannot open shared object file: No such file or directory
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/home/sunxd/R/x86_64-pc-linux-gnu-library/3.3/rJava’

The downloaded source packages are in
Warning message:
In install.packages("rJava", type = "source") :
  installation of package ‘rJava’ had non-zero exit status

Simply start by installing r-cran-rjava from terminal

$ sudo apt-get install r-cran-rjava

Then from R, simply run this

  • 1
    In R 3.6.1, I had to get that package from a PPA (credit: askubuntu.com/a/1116015/48105), then logout/login and restart my R session before rJava could finally be installed. – Waldir Leoncio Dec 3 '19 at 11:01
  • In R 4.*, use this first sudo add-apt-repository ppa:c2d4u.team/c2d4u4.0+ – Qinsi Jan 13 at 2:10

I tried several of the above options, but nothing worked for me.
Then I tried

install.packages("rJava",dependencies = TRUE)

and it worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.