16

Hi I'm having issues with the rJava package from cran.

I have installed

sudo apt-get install openjdk-7-jdk
sudo apt-get install r-cran-rjava

and ran

sudo R CMD javareconf
# Java interpreter : /usr/bin/java
# Java version     : 1.7.0_55
# Java home path   : /usr/lib/jvm/java-7-openjdk-amd64/jre
# Java compiler    : /usr/bin/javac
# Java headers gen.: /usr/bin/javah
# Java archive tool: /usr/bin/jar

I then try to run R and load rJava and get the following error:

R
> library(rJava)
Error : .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
Error: package or namespace load failed for ‘rJava’

I'm on Ubuntu 14.04 64 bit and am using R version 3.1.0 (2014-04-10) -- "Spring Dance"

UPDATE: Actually this is not specific to OpenJDK, I just tried oracle java 8 and got the same result. Also I found this workaround here which I am reluctant to use since it is indeed a workaround and doesn't really explain why it's necessary. The package system should have handled this in my opinion. Seems like libjvm.so is the problem and I have it located here

/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/jamvm/libjvm.so
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/libjvm.so
/usr/lib/jvm/java-7-oracle/jre/lib/amd64/server/libjvm.so

and for some reason rJava fails to find them despite updating with sudo R CMD javareconf.

UPDATE 2: The plot thickens: If I run R as sudo it works.

Thankful for pointers.

Community
  • 1
  • 1
Dr. Mike
  • 2,451
  • 4
  • 24
  • 36

6 Answers6

10

you can solve this problem by opening rstudio in super user like

sudo rstudio

then inside R run

install.packages('rJava')
Rodrigo
  • 4,706
  • 6
  • 51
  • 94
sagarambat
  • 101
  • 1
  • 2
  • Much simpler solution, it also worked for me. Only that "type..." is not a terminal command, so should be formatted differently. Thank you. – Rodrigo Jul 31 '15 at 13:22
  • If u use RStudio Server the command sudo rstudio doesnt apply – Patrik_P Mar 03 '17 at 07:32
7

I had the same problem with a similar configuration (R 3.1.0, Ubuntu 12.10, 32-bit). I found the answer was in getting LD_LIBRARY_PATH set properly, as described here: error: unable to load installed packages just now except that the subdirectory in question is 'client' not 'server'. So now I'm setting my environment like this:

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/i386:$JAVA_HOME/jre/lib/i386/client
Community
  • 1
  • 1
  • That does indeed solve the issues I've been having. The only question remaining is why LD_LIBRARY_PATH is not set automatically? But anyway, like I said this fixed the problem for me. Thanks. – Dr. Mike May 11 '14 at 10:02
  • Worked for me on ubuntu server 14.04 `export LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/:/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server` – Sudipta Basak Aug 23 '16 at 12:48
7

I was able to solve this permanentelly using this answer: https://stackoverflow.com/a/25932828/3939832

This is usefull if you have Oracle java 7 or 8 installed. Exporting variables in my case was not a permanent solution. You should check what R is using as environment by doing:

Sys.getenv("JAVA_HOME")

and then you can use that environment by creating a java.conf file on /etc/ld.so.conf.d/ as stated in the above answer link.

Community
  • 1
  • 1
lapisdecor
  • 243
  • 2
  • 10
6

if you are using oracle java then use following command :

sudo R CMD javareconf

won't help use:

sudo R CMD javareconf **JAVA_HOME**=(path where java home is located)
Nomesh DeSilva
  • 1,649
  • 4
  • 25
  • 43
sourav karwa
  • 101
  • 1
  • 3
1

I tried many things but didnt worked. Then I tried using

sudo rstudio

and then

install.packages('rJava')

Its working. Coool

Robert
  • 5,278
  • 43
  • 65
  • 115
Ajay Jadhav
  • 161
  • 1
  • 1
  • 5
  • This worked for me, but I had to do ´sudo R CMD javareconf´ in a terminal before installing rJava. – Gorka Jan 26 '18 at 13:53
1

Installing the rJava package on Ubuntu is not quite as simple as most other R packages. Some quick notes on how to do it(Source: https://www.r-bloggers.com/installing-rjava-on-ubuntu/).

Install the Java Runtime Environment (JRE).

sudo apt-get install -y default-jre

Install the Java Development Kit (JDK).

sudo apt-get install -y default-jdk

Update where R expects to find various Java files.

sudo R CMD javareconf

Install the package.

> install.packages("rJava")

If you have a RStudio session open, then exit and restart it. This is important (a running RStudio session will not pick up these changes!).
Jyoti
  • 13
  • 3