-->

R package rJava only loads if I launch R with sudo

2019-05-07 11:12发布

问题:

I'm running R 3.0.2 on Ubuntu 14.04.1. I've installed the rJava package, but I can only get it to work in R/RStudio if I launch it with sudo. If I click on the application launcher, or just enter R in the command line, I get the following error when loading the rJava library:

> 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 director  
Error: package or namespace load failed for ‘rJava’

However, if I type 'sudo rstudio' or 'sudo R' in a terminal, rJava loads without a problem. Does anybody know what I'm doing wrong?

Possibly related issue: I installed rJava by entering

sudo apt-get install r-cran-rjava

at the command prompt; installing the normal way:

apt-get install r-cran-rjava

did not work for me, and neither did installing from within R (without launching it using sudo). When I tried to install this way, I ran into the same problem as addressed in this question.

回答1:

The file /usr/lib/R/site-library/rJava/libs/rJava.so is probably not readable by any user other than root. You can check that with:

ls -l /usr/lib/R/site-library/rJava/libs/rJava.so

If the output is rw------- or rw-rw---- then simple users won't be able to read it. You can correct that with the chmod command:

chmod -R a+rX /usr/lib/R/site-library/rJava/


回答2:

I tried the solution listed here to no avail. What worked on Ubuntu 14.04 was to create a soft link to libjvm.so in /usr/lib.

ln -s /usr/lib/jvm/java-8-oracle/jre/lib/amd64/server/libjvm.so /usr/lib 


回答3:

You need to unset the JAVA_HOME environmental variable before starting R:

 unset JAVA_HOME


回答4:

Saw this problem when running from Rscript. Sudo worked, Rscript as user did not. I first did chmod -R a+rX as damienfrancois suggested, but that did not work. Then I tried unset JAVA_HOME, as Gon suggested. That worked (or possibly that in combination with the chmod).



标签: r ubuntu rjava