R: rJava package install failing

2019-01-01 08:41发布

When installing rJava using the install.packages("rJava") command I get the following error:

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-6-sun-1.6.0.20/jre/../include -I/usr/lib/jvm/java-6-sun-1.6.0.20/jre/../include/linux'
java libs   : '-L/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/amd64/server -L/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/amd64 -L/usr/lib/jvm/java-6-sun-1.6.0.20/jre/../lib/amd64 -L -L/usr/java/packages/lib/amd64 -L/usr/lib64 -L/lib64 -L/lib -L/usr/lib -ljvm'
checking whether JNI programs can be compiled... yes
checking JNI data types... configure: error: One or more JNI types differ from the corresponding native type. You may need to use non-standard compiler flags or a different compiler in order to fix this.
ERROR: configuration failed for package ‘rJava’

I have the Java JDK installed and java -version returns the following:

$ java -version
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)

When Googling around for the error I see that others are having the same issue but not finding a solution other than "install the whole JDK, not just the JRE" which I have done.

The other thing I read was to run sudo R CMD javareconf which runs quite happily with no errors.

Any ideas what my problem is?

[[EDIT]] It's been a few months since I had this problem. I had initially solved this by editing my Java paths, as illustrated in the answer I posted below. I recently ran into the same issue on a new Ubuntu install. I tried Dirk's recommendation to use apt-get to install the rJava package. It worked perfectly. What I failed to appreciate initially is that installing packages using the Ubuntu apt-get method is fundamentally different than just loading the same package using install.packages() inside of R. The Ubuntu packages solve some issues which I didn't realize or appreciate.

标签: r rjava
15条回答
梦醉为红颜
2楼-- · 2019-01-01 09:07

The rJava package looks for the /usr/lib/jvm/default-java/ folder. But it's not available as default. This folder have a symlink for the default java configured for the system.

To activate the default java install the following packages:

sudo apt-get install default-jre default-jre-headless

Tested on ubuntu 17.04 with CRAN R 3.4.1

查看更多
冷夜・残月
3楼-- · 2019-01-01 09:09

That is how I make it work :

In Linux (Ubuntu 16.04)

sudo apt-get install default-jre
sudo apt-get install default-jdk
sudo R CMD javareconf

in R:

install.packages("rJava")
查看更多
无与为乐者.
4楼-- · 2019-01-01 09:11

I tried to install openjdk-7-* but still I had problems installing rJava. Turns out after I restarted my computer, then there was no problem at all.

so

sudo apt-get install openjdk-7-*


RESTART after installing java, then try to install package "rJava" in R
查看更多
登录 后发表回答