129

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.

4
  • answer can be found here also -stackoverflow.com/questions/12872699/… Commented Dec 2, 2015 at 13:46
  • 1
    Just making a comment here to my future self (for when I undoubtably find myself back at this question) that I needed to run sudo R CMD javareconf to get the apt-get install to be recognized my rstudio-server. Commented Sep 13, 2017 at 14:03
  • 4
    Nine years later and this a never ending problem. I run into this every time I do a new installation of R. Something changes.. OS, Rstudio version, R version, java version, jdk, jre, jvm.... And it's always a new solution to solving it. It's ridiculous that I cannot read an excel file without all this bullshit. Commented Feb 18, 2019 at 15:38
  • @rmf there are now a number of excel reading packages that don't require Java. For simple reads I like readxl but for writing back into Excel I prefer openxlsx. Neither requires rJava. Commented Feb 20, 2019 at 1:13

18 Answers 18

132

Wouldn't

apt-get install r-cran-rjava 

have been easier? You could have asked me at useR! :)

Sign up to request clarification or add additional context in comments.

17 Comments

The point of Ubuntu and Debian is that you should't have to google in the first place --- it should "just work" using the proper packages. Make your life easier and use packages where you can. I presume you used Ubuntu's Java packages too?
Dirk, you ask these questions like I have any idea what the hell I did. If I recall, I had trouble installing Java using apt-get so I built from source and that's probably the source (all puns intended) of my problems. Of course it might just be bad karma.
I am on R 2.15.3 ubuntu 12.04.2 64bit. when I update.packages() I get i notice that there is a rJava update. Installation from within R fails. When I apt-get install r-cran-rjava there is no update available. Why is this happening?
@DirkEddelbuettel, In my Red Hat 'sudo yum install r-cran-rjava' is not working. Says Loaded plugins: rhnplugin This system is receiving updates from RHN Classic or RHN Satellite. Setting up Install Process No package r-cran-rjava available.
@DirkEddelbuettel, while the 'it should just work' approach sounds great to me, the fact that only so many packages are listed in r-cran-x is why people use the 'regular' R way (install.packages). Moreover, I just took your advise on doing it through cran, which really doesn't help as R 3.x is mainstream now, while packages in r-cran- are apparently before R 3.x
|
80

Turns out my problem was an issue with my JAVA_HOME environment variable. Yes, shocking I know. My initial setting for PATH and JAVA_HOME looked like this:

export JAVA_HOME=/usr/lib/jvm/java-6-sun export PATH=$PATH:$JAVA_HOME/bin 

And I added /jre so it now looks like this:

export JAVA_HOME=/usr/lib/jvm/java-6-sun/jre export PATH=$PATH:$JAVA_HOME/bin 

Everything in Java seemed to work fine without the /jre but rJava would not. Odd.

5 Comments

Keeping to the true meaning of "stack overflow" by creating a question -> answer infinite loop---I like it.
Thanks. Doing my bit to make sure future Google users can find answers to their R questions!
After modifying JAVA_HOME and the path to the java executable, I found I need to run R CMD javareconf. Otherwise, the rJava installation process still sees the old locations of headers, and libraries, etc.
Nothing worked for me. I am trying to install "rJava" in the Red Hat Linux server where I am serving some R Shiny applications. I am getting the following ERROR: configuration failed for package ‘rJava’ * removing ‘/usr/lib64/R/library/rJava’. I have done the following steps in the process of eliminating the error. 1. Updated Java in Linux sudo yum install java-1.7.0-openjdk 2. Set the HOME and PATH export JAVA_HOME=/usr/lib/jvm/java-1.7.0-ibm-1.7.0.6.1.x86_64/jre export PATH=$PATH:$JAVA_HOME/bin 3. Reconfigured Java sudo /usr/bin/R CMD javareconf
Yes, it works, I used the 8-oracle version instead.
36

That is how I make it work :

In Linux (Ubuntu 16.04 and 20.04 worked confirmed)

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

in R:

install.packages("rJava") 

6 Comments

thanks ... I needed to install something called rcdk in R which fell down 'cos java didn't work. Your work around solved the problem (Ubuntu 18.04)
Thanks. Good tip. I was able to fix the build of a Vagrant Machine in xenial64.
How to handle the issue without sudo permission?
Worked on Ubuntu 20.04 too.
Worked on Ubuntu 14.04 too.
|
22

Thanks - your suggestion about $JAVA_HOME lead me to a similar solution:

unset JAVA_HOME 

before invoking R.

3 Comments

unset JAVA_HOME (no dollar sign)
Yes, this fixed my problem. Can you please explain why? Thanks
unset JAVA_HOME and set CLASSPATH=$CLASSPATH:/usr/lib64/R/share/java
15

I came across the same issue, and it worked after running commands below.

export JAVA_LIBS="$JAVA_LIBS -ldl" R CMD javareconf 

See details at http://www-01.ibm.com/support/knowledgecenter/SSPT3X_3.0.0/com.ibm.swg.im.infosphere.biginsights.install.doc/doc/install_install_r.html

Comments

11

This worked for me on Ubuntu 12.04 and R version 3.0

cd /usr/lib/jvm/java-6-sun-1.6.0.26/include 

this is the directory that has jni.h

Next create a soft link to another required header file (I'm too lazy to find out how to include more than one directory in the JAVA_CPPFLAGS option below):

sudo ln -s linux/jni_md.h . 

Finally

sudo R CMD javareconf JAVA_CPPFLAGS=-I/usr/lib/jvm/java-6-sun-1.6.0.26/include 

2 Comments

sudo R CMD javareconf JAVA_CPPFLAGS=-I/usr/lib/jvm/java-6-sun-1.6.0.26/include solved the problem for me.
…and I'd add echo $JAVA_HOME in case java-6-sun is not your exact directory.
6

below is one of my answers on another post - error: unable to load installed packages just now
(this is also relevant to this question)

For Linux(Ubuntu) users: If you have oracle-java (7/8) installed. It'll be at this location /usr/lib/jvm and sudo access is required.

Create the file /etc/ld.so.conf.d/java.conf with the following entries:

/usr/lib/jvm/java-8-oracle/jre/lib/amd64 /usr/lib/jvm/java-8-oracle/jre/lib/amd64/server 

(Replace java-8-oracle with java-7-oracle depending on your java version)

Then:

sudo ldconfig 

Restart RStudio and then install the rJava package.

1 Comment

I've tried multiple solutions with varying degrees of success, but this is the first solution I've come across that appears to be resilient (i.e. doesn't require the same 5 steps to be repeated every time I launch R).
4

Running R under Gentoo on an AMD64. I upgraded to R 2.12.0

R version 2.12.0 (2010-10-15) Copyright (C) 2010 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: x86_64-pc-linux-gnu (64-bit) and those pesky messages went away.

Jan Vandermeer

Comments

2

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 

Comments

2

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

Comments

2

What worked for me was changing JAVA_HOME from file /usr/lib/R/etc/javaconf

I first checked what was my version of Java enabled : sudo update-alternatives --config java. In my case, it was java-8-oracle

I opened the file /usr/lib/R/etc/javaconf and replaced default-java by java-8-oracle :

${JAVA_HOME=/usr/lib/jvm/default-java} 

replaced by :

${JAVA_HOME=/usr/lib/jvm/java-8-oracle} 

And then sudo R CMD javareconf

I restarted RStudio, and could then install rJava.

Comments

1

what I do is here:

  1. in /etc/apt/sources.list, add:

    deb http://ftp.de.debian.org/debian sid main

Note:the rjava should be latest version

2 run: sudo apt-get update sudo apt-get install r-cran-rjava

Once update the old version of rjava, then can install rhdfs_1.0.8.

Comments

1

I got it working by downloading : https://cran.r-project.org/src/contrib/rJava_0.9-8.tar.gz and running command R CMD install rJava_0.9-8.tar.gz

Comments

1

I was facing the same problem while using Windows 10. I have solved the problem using the following procedure

  1. Download Java from https://java.com/en/download/windows-64bit.jsp for 64-bit windows\Install it
  2. Download Java development kit from https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html for 64-bit windows\Install it
  3. Then right click on “This PC” icon in desktop\Properties\Advanced system settings\Advanced\Environment Variables\Under System variables select Path\Click Edit\Click on New\Copy and paste paths “C:\Program Files\Java\jdk1.8.0_201\bin” and “C:\Program Files\Java\jre1.8.0_201\bin” (without quote) \OK\OK\OK

Note: jdk1.8.0_201 and jre1.8.0_201 will be changed depending on the version of Java development kit and Java

  1. In Environment Variables window go to User variables for User\Click on New\Put Variable name as “JAVA_HOME” and Variable value as “C:\Program Files\Java\jdk1.8.0_201\bin”\Press OK

To check the installation, open CMD\Type javac\Press Enter and Type java\press enter It will show enter image description here

In RStudio run

Sys.setenv(JAVA_HOME="C:\\Program Files\\Java\\jdk1.8.0_201") 

Note: jdk1.8.0_201 will be changed depending on the version of Java development kit

Now you can install and load rJava package without any problem.

Comments

1

On Arch Linux, I needed to install openjdk-src to get a JNI path working.

In other words, these are the packages I needed to install before sudo R CMD javareconf ran successfully:

local/jdk-openjdk 14.0.2.u12-1 OpenJDK Java 14 development kit local/jre-openjdk 14.0.2.u12-1 OpenJDK Java 14 full runtime environment local/jre-openjdk-headless 14.0.2.u12-1 OpenJDK Java 14 headless runtime environment local/openjdk-src 14.0.2.u12-1 OpenJDK Java 14 sources 

Comments

0

The problem was rJava wont install in RStudio (Version 1.0.136). The following worked for me (macOS Sierra version 10.12.6) (found here):

Step-1: Download and install javaforosx.dmg from here

Step-2: Next, run the command from inside RStudio:

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

Comments

0

Assuming you have sudo privileges and not in Ubuntu where package manager makes this easier -- I tried variations of prior answers and found this gem on Databricks blog for nonUbuntu (https://kb.databricks.com/r/install-rjava-rjdbc-libraries.html)

  1. Installed JDK
sudo apt-get install openjdk-8-jdk 
  1. Verify path to libjvm.so; for me-->
cd /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64 grep -r libjvm 

output:

Binary file server/libjvm.so matches <<<<<<<< 
  1. Do java configure
 sudo R CMD javareconf 
  1. Remove prior versions of the package and install 'rJava' from CRAN mirror in RStudio

  2. Restart RStudio

  3. In RStudio verify link to libjvm.so

 dyn.load('/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/libjvm.so') library('rJava') 

Comments

-2

I've encountered similar problem on Ubuntu 16.04 and was able to solve it by creating a folder named "default-java" in /usr/lib/jvm and copying into it all the contents of the /usr/lib/jvm/java-8-oracle. I opted for this solution as correcting JAVA_HOME environment variable turned out to be of no use.

1 Comment

This will break the default-jre package install.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.