Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does rJava not work on Ubuntu 14.04 using OpenJDK 7?

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.

like image 475
Dr. Mike Avatar asked May 05 '14 09:05

Dr. Mike


People also ask

What is rJava?

rJava is a JNI (Java Native Interface)-based, R-to-Java interface with which Java objects may be created and Java methods called and Java fields accessed from R. In this tutorial, we shall get started with using the rJava R package to use Java from R. This tutorial has the following sections: Setting the Environment.

What is rJava package?

rJava provides a low-level bridge between R and Java (via JNI). It allows to create objects, call methods and access fields of Java objects from R. rJava release versions can be obtained from CRAN - usually install. packages("rJava") in R will do the trick.


2 Answers

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

sudo rstudio

then inside R run

install.packages('rJava')
like image 167
sagarambat Avatar answered Oct 21 '22 07:10

sagarambat


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
like image 25
persistentNoob Avatar answered Oct 21 '22 07:10

persistentNoob