在RStudio-server中安装rJava,遇到如下问题
cpp flags : '-I/usr/lib/jvm/java-11-openjdk-amd64/include -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux'
java libs : '-L/usr/lib/jvm/java-11-openjdk-amd64/lib/server -ljvm'
checking whether Java run-time works... yes
checking whether -Xrs is supported... yes
checking whether -Xrs will be used... yes
checking whether JVM will be loaded dynamically... no
checking whether JNI programs can be compiled... configure: error: Cannot compile a simple JNI program. See config.log for details.
Make sure you have Java Development Kit installed and correctly registered in R.
If in doubt, re-run "R CMD javareconf" as root.
ERROR: configuration failed for package ‘rJava’
* removing ‘/home/xzg/R/x86_64-pc-linux-gnu-library/4.3/rJava’
ERROR: dependency ‘rJava’ is not available for package ‘xlsxjars’
* removing ‘/home/xzg/R/x86_64-pc-linux-gnu-library/4.3/xlsxjars’
ERROR: dependencies ‘rJava’, ‘xlsxjars’ are not available for package ‘xlsx’
* removing ‘/home/xzg/R/x86_64-pc-linux-gnu-library/4.3/xlsx’
The downloaded source packages are in
‘/tmp/Rtmpkx8xeJ/downloaded_packages’
Warning messages:
1: In install.packages("xlsx") :
installation of package ‘rJava’ had non-zero exit status
2: In install.packages("xlsx") :
installation of package ‘xlsxjars’ had non-zero exit status
3: In install.packages("xlsx") :
installation of package ‘xlsx’ had non-zero exit status
按照建议,以root运行R CMD javareconf
,并在终端顺利安装rJava和xlsx,但是在Rstudio-server中依旧无法正常安装或者是加载rJava,如下所示
> library(xlsx)
Error: package or namespace load failed for ‘xlsx’:
.onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/home/xzg/R/x86_64-pc-linux-gnu-library/4.3/rJava/libs/rJava.so':
libjvm.so: cannot open shared object file: No such file or directory
该问题,无法通过简单复制libjvm.so以及其他so文件来解决,并且GPT给的建议几乎都无效,只有一条”检索相关社区“是有用的,最后,发现,解决方案如下
sudo rstudio-server stop
R CMD javareconf
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
export LD_LIBRARY_PATH=$JAVA_HOME/lib/amd64:$LD_LIBRARY_PATH
sudo rstudio-server start
也就是要先关闭RStudio-server,然后配置好环境变量,重启启动后,RStudio-server就携带了Java的相关配置文件,如此才能正常安装和使用rJava。