You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R 4.3.1安装rJava报错:无法编译简单JNI程序(无sudo权限)

安装rJava包遇到的问题及解决思路

环境信息

  • R版本:4.3.1
  • 操作系统:Red Hat/CentOS 8
  • OpenJDK版本:"16.0.2" 2021-07-20

安装报错详情

执行install.packages('rJava')时出现以下错误:

> install.packages('rJava')
* installing *source* package ‘rJava’ ...
** package ‘rJava’ successfully unpacked and MD5 sums checked
** using staged installation
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
checking for sys/wait.h that is POSIX.1 compatible... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for string.h... (cached) yes
checking sys/time.h usability... yes
checking sys/time.h presence... yes
checking for sys/time.h... yes
checking for unistd.h... (cached) yes
checking for an ANSI C-conforming const... yes
checking whether time.h and sys/time.h may both be included... yes
configure: checking whether gcc supports static inline...
yes
checking whether setjmp.h is POSIX.1 compatible... yes
checking whether sigsetjmp is declared... yes
checking whether siglongjmp is declared... yes
checking Java support in R... present:
interpreter : '/usr/bin/java'
archiver    : '/usr/bin/jar'
compiler    : '/usr/bin/javac'
header prep.: '/usr/bin/javah'
cpp flags   : '-I/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-2.el8.x86_64/jre/../include -I/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-2.el8.x86_64/jre/../include/linux'
java libs   : '-L/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-2.el8.x86_64/jre/lib/amd64/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.

问题与解决方法

1. config.log文件位置

源码安装R包时,config.log会生成在临时编译目录中。可以通过以下方式定位:

  • 执行安装命令时增加verbose=TRUE参数,查看输出中的临时目录路径:
    install.packages('rJava', verbose = TRUE)
    
  • 临时目录通常类似/tmp/RtmpXXXXXX/downloaded_packages/rJava(XXXXXX为随机字符),安装失败后该目录不会立即删除,需尽快查看。

2. 无sudo权限的解决方案

方法一:手动指定Java环境变量

在R会话中设置对应环境变量,确保R指向正确的OpenJDK 16路径(替换为你的实际路径):

Sys.setenv(JAVA_HOME = "/usr/lib/jvm/java-16-openjdk-16.0.2.0.7-1.el8.x86_64")
Sys.setenv(LD_LIBRARY_PATH = paste0(Sys.getenv("LD_LIBRARY_PATH"), ":", Sys.getenv("JAVA_HOME"), "/lib/server"))
Sys.setenv(CPPFLAGS = paste0("-I", Sys.getenv("JAVA_HOME"), "/include -I", Sys.getenv("JAVA_HOME"), "/include/linux"))
Sys.setenv(LDFLAGS = paste0("-L", Sys.getenv("JAVA_HOME"), "/lib/server -ljvm"))

设置完成后重新执行安装:

install.packages('rJava', type = "source")

方法二:尝试预编译二进制包

联系管理员,请求通过系统包管理器安装EPEL仓库中的R-rJava预编译包,避免源码编译的依赖问题。

方法三:确认Java开发依赖

检查你的OpenJDK目录下是否存在include/jni.h等开发头文件。若缺失,需联系管理员安装对应版本的java-16-openjdk-devel包,这是JNI编译必需的依赖。

内容的提问来源于stack exchange,提问作者jeje

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 10:13:19