38

I'm trying to install rJava in a docker container based on debian (jessie) with Anaconda.

As root, I did

$ apt-get update  && apt-get install -y --no-install-recommends \
  default-jdk default-jre libicu-dev 

Then

$ R CMD javareconf 
Java interpreter : /usr/lib/jvm/jdk1.8.0_121/jre/bin/java
Java version     : 1.8.0_121
Java home path   : /usr/lib/jvm/jdk1.8.0_121
Java compiler    : /usr/lib/jvm/jdk1.8.0_121/bin/javac
Java headers gen.: /usr/lib/jvm/jdk1.8.0_121/bin/javah
Java archive tool: /usr/lib/jvm/jdk1.8.0_121/bin/jar

trying to compile and link a JNI program 
detected JNI cpp flags    : -I/usr/lib/jvm/java/include -I/usr/lib/jvm/java/include/linux
detected JNI linker flags : -L$(JAVA_HOME)/jre/lib/amd64/server -ljvm
gcc -std=gnu99 -I/opt/conda/lib/R/include -DNDEBUG -I/usr/lib/jvm/java/include -I/usr/lib/jvm/java/include/linux -I/opt/conda/include    -fpic  -I/opt/conda/include  -c conftest.c -o conftest.o
conftest.c:1:17: fatal error: jni.h: No such file or directory
 #include <jni.h>
                 ^
compilation terminated.
/opt/conda/lib/R/etc/Makeconf:133: recipe for target 'conftest.o' failed
make: *** [conftest.o] Error 1
Unable to compile a JNI program


JAVA_HOME        : /usr/lib/jvm/jdk1.8.0_121
Java library path: 
JNI cpp flags    : 
JNI linker flags : 
Updating Java configuration in /opt/conda/lib/R
Done.

But jni.h is right in the JDK:

# find /usr/lib/jvm/jdk1.8.0_121/ -name jni.h
/usr/lib/jvm/jdk1.8.0_121/include/jni.h

It just doesn't look like javareconf is finding it. And, if I look at all the variables I could set with javareconf, like JAVA_HOME, I don't see the ability to set JNI cpp flags, which would need the right includes.

I've seen various posts around the Internet of this problem, but no solutions.

dfrankow
  • 20,191
  • 41
  • 152
  • 214

8 Answers8

29

I'm not sure if an answer has been confirmed here, but I would point everyone to this article by Andrew Collier.

What he recommends:

(1) Updating all repositories

sudo apt update -y

(2) Being sure JRE and JDK 8 are installed

sudo apt install -y openjdk-8-jdk openjdk-8-jre

(3) Then, as many users have said already, pointing R to java with an explicit path to JDK 8

sudo R CMD javareconf JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/

The specific Java package does not matter too much as long as it has all the necessary compiled contents within the /include directory.

user@laptop:~$ ls /usr/lib/jvm/java-8-openjdk-amd64/include/
classfile_constants.h  jdwpTransport.h  jvmticmlr.h  linux
jawt.h                 jni.h            jvmti.h

For more on this, I would recommend how to make jni.h be found? for going about locating/compiling this directory correctly.

  • Not pointing to an explicit path was the problem in my case. This solution worked for me in Ubuntu 18.04 using R 3.6.3. – Lfppfs Oct 27 '21 at 14:23
16

R is looking at different location:

detected JNI cpp flags    : -I/usr/lib/jvm/java/include -I/usr/lib/jvm/java/include/linux

I experienced similar problem, but everything is fine after installing jdk:

sudo apt-get install openjdk-8-jdk
sgu
  • 1,301
  • 1
  • 13
  • 25
  • it might be useful to check first if jdk is installed but not correctly referenced, see Damiano Fantini`s answer. If not your way is very valuable, thanks! – Tonio Liebrand Nov 03 '19 at 08:32
12

Had the same issue. Fixed by specifying the path to the Java folder. Briefly, after installing Java you may get a line like this.

update-alternatives: using /usr/lib/jvm/java-9-openjdk-amd64/bin/jar to provide /usr/bin/jar (jar) in auto mode

I specified that path as JAVA_HOME argument when calling javareconf, as follows.

sudo R CMD javareconf JAVA_HOME=/usr/lib/jvm/java-9-openjdk-amd64/bin/jar

Hope this is useful

Damiano Fantini
  • 1,925
  • 9
  • 11
  • 1
    wow this saved me after like 3 hours of browsing through R, Java, rJava and macOS questions... I had exported my JAVA_HOME but when i called R CMD javareconf it was still using a different path. – jtanman Jun 01 '18 at 02:24
  • Using Java 8, the following command worked for me: `sudo R CMD javareconf JAVA_HOME=/usr/lib/jvm/adoptopenjdk-8-hotspot-amd64`. (I used https://stackoverflow.com/a/59436618/2251463 to install Java 8 on Debian 10 Buster). – Shafique Jamal Jun 12 '20 at 11:17
7

I solved the problem by editing the java-config file

sudo nano /usr/lib/R/etc/javaconf

set the correct JAVA_HOME in this row:
: ${JAVA_HOME=/usr/local/java/jdk-9.0.1}
MLavoie
  • 9,671
  • 41
  • 36
  • 56
2

I already had Java 11 on ubuntu 18.04 but had to install jdk-11.0.1 following this guide: https://websiteforstudents.com/how-to-install-oracle-java-jdk-11-on-ubuntu-18-04-16-04-18-10/

I then changed JAVA_HOME in javaconf, just like @Spanska Forteva

sudo nano /usr/lib/R/etc/javaconfIn javaconf 

I found that the path to an old version of java was still specified.

## Versions from settings when configure was run
: ${JAVA_HOME=/usr/lib/jvm/default-java}
: ${JAVA_CPPFLAGS=~autodetect~}
: ${JAVA_LD_LIBRARY_PATH=~autodetect~}
: ${JAVA_LIBS=~autodetect~}

I changed the path to JAVA_HOME to my newly installed jdk:

: ${JAVA_HOME=/usr/lib/jvm/jdk-11.0.1}

Then ran the CMD with the path to jni.h specified:

sudo R CMD javareconf /usr/lib/jvm/jdk-11.0.1/include/jni.h

The last bit might not be needed, it should be able to look in that folder anyway, but that is what I did.

chilifan
  • 157
  • 1
  • 12
2

Had the same problem and could finally fix it. After a lot of searching, this worked for me (I basically followed the guide here: https://www3.ntu.edu.sg/home/ehchua/programming/howto/JDK_Howto.html):

  • downloading and unpacking the JDK (jdk1.8.0_201) to /usr/java/
  • downloading and unpacking the JRE (jre1.8.0_201) to /usr/java/ (not sure if I needed this step but I did it in previous attempts to solve this problem)
  • Informing Ubuntu of the new java using appropriate versions

For this I ran:

$ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/java/jdk1.8.0_201/bin/java" 1

$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/jdk1.8.0_201/bin/javac" 1

$ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/java/jdk1.8.0_201/bin/javaws" 1

followed by

$ sudo update-alternatives --set java /usr/java/jdk1.8.0_201/bin/java

$ sudo update-alternatives --set javac /usr/java/jdk1.8.0_201/bin/javac

$ sudo update-alternatives --set javaws /usr/java/jdk1.8.0_201/bin/javaws

Then I could finally see my java in the alternatives in

sudo update-alternatives --config java

where I selected the newly installed java. Then I ran

 sudo R CMD javareconf JAVA_HOME=/usr/java/jdk1.8.0_201/bin/

in which I specified again my java jdk path explicitely. After this finished successfully I was able to install rJava in Rstudio which I needed to install glmulti.

2

In my case, I had to run

R CMD javareconf

as su, just sudo didn't work.

1

For Ubuntu 22.04 user, openjdk-19-jdk & openjdk-19-jre are available. Installing these fix the issue as well. So better use the latest available version:

sudo apt update -y
sudo apt install -y openjdk-19-jdk openjdk-19-jre
sudo R CMD javareconf JAVA_HOME=/usr/lib/jvm/java-19-openjdk-amd64/
Yoann Pageaud
  • 412
  • 5
  • 22
  • 1
    On ubuntu 22.04 this appears to be the solution. I tried most other suggestions before finding this, but this is the only think that worked. – Reed Merrill Jul 15 '23 at 22:48