Problem with rJava on Rstudio in Anaconda on Cent OS7

Hello,
I have rstudio in jupyter (both R and Jupyter are from anaconda). Unfortunatelly I have problem with rJava package. When I try to load the package I have following error:

Loading required package: rJava
Error: package or namespace load failed for ‘rJava’:
.onLoad failed in loadNamespace() for ‘rJava’, details:
call: dyn.load(file, DLLpath = DLLpath, …)
error: nie można załadować współdzielonego obiektu ‘/opt/anaconda3/envs/R_kernel/lib/R/library/rJava/libs/rJava.so’:
libjvm.so: nie można otworzyć pliku obiektu dzielonego: Nie ma takiego pliku ani katalogu

I try
sudo R CMD javareconf JAVA_HOME=/opt/anaconda3/envs/R_kernel/lib

but still it doesn’t help.
I check also

Sys.getenv(“JAVA_HOME”) which give me ‘/opt/anaconda3/envs/R_kernel’
Sys.getenv(“LD_LIBRARY_PATH”) or Sys.getenv(“LIBRARY_PATH”) give me empty result (only ").

What can I do also to have rJava working properly ?

1 Like

Do you have a JAVA JDK installed? This is required in order for the rJava package to work.
I suggest looking at this discussion for further information:

1 Like

Dear sweller,
Yes I have installed JAVA JDK:

My version:
openjdk version “11.0.15” 2022-04-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.15+9-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.15+9-LTS, mixed mode, sharing)

I also read “rJava fails to load” nad try solutions from there, but unfortunatelly, still it isn’t working.

Can you run the command:
‘R CMD javareconf’

and paste the output below?

Thanks.

If I run command “R CMD javareconf” I have the following output:
(base) [root@percona-1 ~]# R CMD javareconf
*** JAVA_HOME is not a valid path, ignoring
Java interpreter : /usr/bin/java
Java version : 11.0.15
Java home path : /usr/lib/jvm/java-11-openjdk-11.0.15.0.9-2.el7_9.x86_64
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/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64/in clude -I/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64/include/linux
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
gcc -std=gnu99 -I/opt/anaconda3/lib/R/include -DNDEBUG -I/usr/lib/jvm/java-1.7.0 -openjdk-1.7.0.45.x86_64/include -I/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_ 64/include/linux -I/opt/anaconda3/include -fpic -I/opt/anaconda3/include -c conftest.c -o conftest.o
conftest.c:1:17: fatal error: jni.h: Nie ma takiego pliku ani katalogu
#include <jni.h>
^
compilation terminated.
make: *** [conftest.o] Błąd 1
Unable to compile a JNI program

JAVA_HOME : /usr/lib/jvm/java-11-openjdk-11.0.15.0.9-2.el7_9.x86_64
Java library path:
JNI cpp flags :
JNI linker flags :
Updating Java configuration in /opt/anaconda3/lib/R
Done.

But if I run “sudo R CMD javareconf JAVA_HOME= /usr/lib/jvm/java-11-openjdk-11.0.15.0.9-2.el7_9.x86_64/bin/java” I have:

Java interpreter : /bin/java
Java version : 11.0.15
Java home path : /usr/lib/jvm/java-11-openjdk-11.0.15.0.9-2.el7_9.x86_64
Java compiler : /bin/javac
Java headers gen.: /bin/javah
Java archive tool: /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/server -ljvm
gcc -m64 -std=gnu99 -I"/usr/include/R" -DNDEBUG -I/usr/lib/jvm/java-11-openjdk-11.0.15.0.9-2.el7_9.x86_64/include -I/usr/lib/jvm/java-11-openjdk-11.0.15.0.9-2.el7_ 9.x86_64/include/linux -I/usr/local/include -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -gr ecord-gcc-switches -m64 -mtune=generic -c conftest.c -o conftest.o
gcc -m64 -std=gnu99 -shared -L/usr/lib64/R/lib -Wl,-z,relro -o conftest.so conftest.o -L/usr/lib/jvm/java-11-openjdk-11.0.15.0.9-2.el7_9.x86_64/lib/server -ljvm -L /usr/lib64/R/lib -lR

JAVA_HOME : /usr/lib/jvm/java-11-openjdk-11.0.15.0.9-2.el7_9.x86_64
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /usr/lib64/R
Done.

If I don’t use sudo in the command ( so it will be something like this: " R CMD javareconf JAVA_HOME= /usr/lib/jvm/java-11-openjdk-11.0.15.0.9-2.el7_9.x86_64/bin/java" )I will have the same error like when I write “R CMD javareconf”

This looks like a pathing problem. I think you need to set JAVA_HOME’ as the user you are logged in as, while NOT running the ‘sudo’ command. Check your
“PATH” environment variable as well and compare it to when you are running R CMD javreconf using ‘sudo’.

Hope this helps.