R 之 rJava 包安装错误的解决方案

前几天在Ubuntu上安装R中的xlsx包时一直卡在了rJava包的安装上,最终各种google都没能解决问题.直到最后,我回到了安装rJava时的错误记录....我用血的教训证明,错误日志是很重要很重要的!!!!!

(由于已经成功地排除了错误,且不打算折腾我的电脑,我就从网上copy一个类似的来说明下发生了什么问题)

运行 install.packages("rJava")并下载解压缩后,日志显示电脑上JDK环境配置有问题.(一大堆的yes后在"checking Java support in R"那里卡住了)

如下:

checking Java support in R... present:
interpreter : '/usr/bin/java'
archiver    : ''                       #空白
compiler    : '/usr/bin/javac'
header prep.: ''                      #空白
cpp flags   : '-I/usr/share/jdk1.8.0_51/jre/../include -I/usr/share/jdk1.8.0_51/jre/../include/linux'
java libs   : '-L/usr/share/jdk1.8.0_51/jre/lib/amd64/server -ljvm'
configure: error: One or more Java configuration variables are not set.
Make sure R is configured with full Java support (including JDK). Run         #注意此句:JDK环境有误
R CMD javareconf
as root to add Java support to R.                   # 这句告诉我们,在确保JDK的安装路径没有问题的情况下应该如何就行修复

If you don't have root privileges, run
R CMD javareconf -e
to set all Java-related variables and then install rJava.

ERROR: configuration failed for package ‘rJava’                          #由于安装出错,移除安装包
* removing ‘/home/cc886/R/x86_64-pc-linux-gnu-library/3.1/rJava’
Warning in install.packages :
  installation of package ‘rJava’ had non-zero exit status

The downloaded source packages are in
	‘/tmp/RtmpaX9v6v/downloaded_packages’

事实上我在翻看错误记录看到那两段空白的时候,终于意识到了问题的关键. 一开始在Ubuntu上配置的是用apt-get获取的openjdk环境,无论我如何运行R CMD javareconf 都没能修复这个问题.

最后,在尝试着上官网下载sunjdk并进行配置的过程中,我发现openjdk和sunjdk相比少了一些文件,即bin文件夹里的javac,javah等等.而Javac立刻让我联想到了错误日志里的"compliler"的空白.于是我用sudo update-alternatives --install ***** 对jar,javah等进行了手动的指定.并发现那两段空白被填补上了.也就是说,这个安装错误信息出现的根本原因就是缺少了jar,javah等的指定(前提是有这些文件)

如果不对jar,javah进行手动指定,运行sudo R CMD javareconf过程中Java headers gen和Java archive tool项会缺失,如下

cc886@cc886:~$ sudo R CMD javareconf -e
[sudo] password for cc886: 
Java interpreter : /usr/bin/java
Java version     : 1.8.0_51
Java home path   : /usr/share/jdk1.8.0_51/jre
Java compiler    : /usr/bin/javac
Java headers gen.:                 #缺失
Java archive tool:                  #缺失

trying to compile and link a JNI progam 
detected JNI cpp flags    : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/linux
detected JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/share/jdk1.8.0_51/jre/../include -I/usr/share/jdk1.8.0_51/jre/../include/linux     -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c conftest.c -o conftest.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o conftest.so conftest.o -L/usr/share/jdk1.8.0_51/jre/lib/amd64/server -ljvm -L/usr/lib/R/lib -lR

The following Java variables have been exported:
JAVA_HOME JAVA JAVAC JAVAH JAR JAVA_LIBS JAVA_CPPFLAGS JAVA_LD_LIBRARY_PATH
Running: /bin/bash

因此,我们需要在手动指定了jar,javah后再运行

sudo R CMD javareconf

在JDK路径无误的状态下,此命令会自动地进行DEBUG.并export一个环境变量 JAVA_TOOL_OPTIONS. 当所有空白被正确填补,并成功运行上述命令后,rJava包即可成功安装.


另,此时运行 java -version会是如下状况,其中的 JAVA_TOOL_OPTIONS一行是rJava的安装所致.

user@loca:~$ java -version
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

具体DEBUG步骤

配置JDK环境

详见另一篇博文:Ubuntu 中 java 环境 (sunjdk) 的配置 (附详细说明)

手动指定archiver, header prep.

# 以下两条应该已经在配置JDK的时候已经运行过了
$ sudo update-alternatives --install /usr/bin/java java /usr/share/jdk1.8.0_51/bin/java 300
$ sudo update-alternatives --install /usr/bin/javac javac /usr/share/jdk1.8.0_51/bin/javac 300

#下面这些是DEBUG所需的
$ sudo update-alternatives --install /usr/bin/jar jar /usr/share/jdk1.8.0_51/bin/jar 300
$ sudo update-alternatives --install /usr/bin/javah javah /usr/share/jdk1.8.0_51/bin/javah 300

指定archiver (jar), header prep (javah)

# config选项指定
$ sudo update-alternatives --config jar 
$ sudo update-alternatives --config javah

运行sudo R CMD javareconfsudo R CMD javareconf -e

按照R中提示运行命令(不知道具体是那个起的作用)

$ sudo R CMD javareconf
$ sudo R CMD javareconf -e

安装rJava包

在R中运行

install.packages("rJava")

一路直踩到底,大功告成!

posted @ 2015-07-28 13:45  Loca  阅读(9258)  评论(0编辑  收藏  举报