34

Trying to install the rJava package on R, on my 14.10 ubuntu, using this code

if (!require(rJava)) install.packages('rJava')

got few errors related to java including jni problems, after reinstalling the jdk, and tried to run the same code again, got that error

Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/usr/local/lib/R/site-library/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 ‘/usr/local/lib/R/site-library/rJava’
Warning in install.packages :
  installation of package ‘rJava’ had non-zero exit status

The downloaded source packages are in
    ‘/tmp/RtmpfFjrp0/downloaded_packages’
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘rJava’

looked for solutions, to find just about everyone suggesting (for x64) to execute those commands

export LD_LIBRARY_PATH=/usr/lib/jvm/java-8-oracle/lib/amd64:/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server
sudo R CMD javareconf 

but that didn't work, is there anything else I can do? I will provide any needed details

tan
  • 439
  • 1
  • 7
  • 10
  • Yes, but I did try that using the terminal command line and Rstudio command line – tan Feb 11 '15 at 21:12
  • `R version 3.5.3 (2019-03-11) Rstudio Version 1.2.1335`. In my case, Just quit the `Rstudio` and restart it. I did not redefine the LD_LIBRARY_PATH. ~~~~ 1. run `sudo R CMD javareconf` 2. install my package. 3. restart Rstudio. – Nick Dong May 24 '19 at 11:20

10 Answers10

29

I was getting the same error when I was trying to load rJava in RStudio. The following solution worked for me

1) sudo rstudio-server stop
2) export LD_LIBRARY_PATH=/usr/lib/jvm/jre/lib/amd64:/usr/lib/jvm/jre/lib/amd64/default
3) sudo rstudio-server start

After performing the above steps, I was able to run the following successfuly in RStudio

> library("DBI")
> library("rJava")
> library("RJDBC")
minhas23
  • 9,291
  • 3
  • 58
  • 40
27

To solve this,

  1. Run

    sudo R CMD javareconf

  2. Add the following to to /etc/environment. This will then be set every time your machine restarts.

    LD_LIBRARY_PATH=/usr/lib/jvm/default-java/jre/lib/amd64/server/

    (change to your path)

  3. Run the following:

    source /etc/environment

  4. rstudio server restart
14

in case none of the above works, try this and thank me later (generic answer) :)

R CMD javareconf -e
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$JAVA_LD_LIBRARY_PATH
Haktan Suren
  • 651
  • 7
  • 17
  • 4
    Oops.. that didn't work either. This did. http://orinanobworld.blogspot.com/2016/12/rjava-gift-that-keeps-on-giving.html – user3673 Jun 20 '17 at 22:57
3

Check if $JAVA_HOME points to the correct location by

echo $JAVA_HOME

I am using openjdk, however $JAVA_HOME was pointing to some non-existing oracle-java folder.

After fixing $JAVA_HOME run

sudo R CMD javareconf

Check the output. Java headers gen. should point to /usr/bin/javah. For example:

djhurio@Skyforger ~ $ sudo R CMD javareconf
Java interpreter : /usr/lib/jvm/default-java/bin/java
Java version     : 11.0.1
Java home path   : /usr/lib/jvm/default-java
Java compiler    : /usr/lib/jvm/default-java/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/lib/jvm/default-java/bin/jar

After this I can install and load rJava package.

djhurio
  • 5,437
  • 4
  • 27
  • 48
2
  • I solved a similar issue by running the following command every time I want to open RStudio. Go to the folder which contains the rstudio executable, open up the terminal and type:

    LD_LIBRARY_PATH=/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/server: open -a rstudio

    Of course you have to change the path according to your Java version.

  • If the previous solution does not work, try reading this article: http://allanino.me/blog/programming/installing-some-r-packages/.

  • If this solution does not work too, the last chance you have is to install rJava compiling it from the source code.

  • 1
    I have encountered this problem when trying to install a nodejs plug-in that has a java dependency. This no JS app is installed in a Heroku environment. Does anyone have any ideas? if so here is a link to the stack overflow: http://stackoverflow.com/questions/36191073/node-java-error-libjvm-so-cannot-open-shared-object-file-no-such-file-or-dire – J0NNY ZER0 Mar 24 '16 at 00:59
2

If someone is coming here now, Please try the approach below:

Steps:

  1. Find your R location. It will be stored in rsession-ld-library-path in rserver.conf file. Or just by doing which R. The location usually is /usr/lib64/R/lib or /usr/lib64/microsoft-r/3.3/lib64/R/lib

  2. Find the libjvm.so file which is usually in the usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server path depending on which jre you're using. Check in $JAVA_HOME environment.

  3. Create a symlink using ln -s sudo ln -s /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/libjvm.so /usr/lib64/microsoft-r/3.3/lib64/R/lib/libjvm.so

  4. Restart R server

0

Well, as @user3673 has pointed out this, the solution for me was change the following line in /usr/lib/R/etc/ldpaths:

: ${R_JAVA_LD_LIBRARY_PATH=${JAVA_HOME}/lib/amd64/server}

by this one:

: ${R_JAVA_LD_LIBRARY_PATH=${JAVA_HOME}/jre/lib/amd64/server}

Fábio
  • 771
  • 2
  • 14
  • 25
0

We are using AZUL. I could see ZST is not added properly. After adding ZST, it works fine.

Rohit
  • 171
  • 1
  • 5
0

Adding the answer if we have two R version running on Unix Box.

Issue: libjvm.so: cannot open shared object file: No such file or directory

Caused due to: Updated the version of java from java-1.8.0-openjdk-1.8.0.262.b10-1.el7_9.x86_64 to java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64

In my case, we had two versions of R

  1. 3.6.0
  2. 3.6.2

we applied the above @minhas23 solution only for 3.6.2 which was set to the default version of R for the Rstudio-server-pro application.

but we were getting a similar error when we select the R version as 3.6.0, then we did the below steps:

export R_VERSION=3.6.0

/opt/R/${R_VERSION}/bin/R --version

unlink /usr/local/bin/R

unlink /usr/local/bin/Rscript

ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R
ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript

rstudio-server stop
export LD_LIBRARY_PATH=/usr/lib/jvm/jre/lib/amd64/server
R CMD javareconf

We have checked the LD_LIBRARY_PATH associated with 3.6.0 as below in the R terminal:

Sys.getenv("LD_LIBRARY_PATH")

O/P

[1] "/opt/R/3.6.0/lib/R/lib:/usr/local/lib:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64/jre/lib/amd64/server:/usr/lib/jvm/jre/lib/amd64/server"
Santosh Garole
  • 1,419
  • 13
  • 23
0

I fixed this for rJava and xlsx in my Dockerfile by searching for the libjvm.so file and setting LD_LIBRARY_PATH to that directory:

RUN find / -name libjvm.so
ENV LD_LIBRARY_PATH=/usr/lib/jvm/java-11-openjdk-amd64/lib/server
Odysseas
  • 41
  • 4