1

I am using Red Hat 5, and unable to install rJava Package

I ran the following command:

$ R CMD javareconf -e

I get the following error:-

trying to compile and link a JNI program detected JNI cpp flags :

detected JNI linker flags :

gcc -m64 -std=gnu99 -I/usr/include/R -DNDEBUG -I/usr/local/include
-fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fpic -fPIC -c conftest.c -o conftest.o

conftest.c:1:17: fatal error: jni.h: No such file or directory

compilation terminated.

make: *** [conftest.o] Error 1

Unable to compile a JNI program


java -version is below:-

java version "1.8.0_91"

Java(TM) SE Runtime Environment (build 1.8.0_91-b14)

Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mod


JAVA_HOME = /usr/java/jdk1.8.0_91/jre

Can anyone please help me to compile JNI Program

3
  • removing the "jre" from JAVA_HOME should help. The include files are in JDK. Jun 8, 2016 at 9:28
  • Try installing all packages related to java with below command yum install java-1.8* this helped in solving above error. Mar 24, 2020 at 6:07
  • @SantoshGarole, but java and jre are already installed Apr 20, 2022 at 15:35

3 Answers 3

0

I had similar problem on Ubuntu. Identify where is the file you are missing

find / -name jni.h 2> /dev/null

probably it will be something like

/usr/lib/jvm/java-8-openjdk-amd64/include

you might also be missing jni_md.h, then run something like below, where the paths are corrected for the ones identified above.

R CMD javareconf JAVA_CPPFLAGS="-I/usr/lib/jvm/java-8-openjdk-amd64/include -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux/"
2
  • I tried /usr/bin/sudo R CMD javareconf JAVACPPFLAGS="-I/usr/lib/jvm/java-1.8.0-ibm-1.8.0.6.35-1jpp.1.el7.x86_64/include" and still got the same error. I confirm that jni.h is in the above folder. Apr 20, 2022 at 15:25
  • what about the second include? In my solution there are two.
    – MPękalski
    May 4, 2022 at 14:15
0

You need to use JDK instead of JRE. JDK contains libs and headers required for JNI compilation.

In general, you have to reconfigure R and make sure to point to proper Java installation.

Take a look here: http://www.owsiak.org/?p=3671 where you can find quite detailed explanation of all the steps you have to take in order to get R properly working with Java. Explanation was prepared for macOS, but it will work for Linux based systems as well - with little modifications.

Just to summarize:

  1. install JDK
  2. reconfigure R

    sudo R CMD javareconf \
    JAVA_HOME=${JAVA_HOME} \
    JAVA=${JAVA_HOME/bin/java \
    JAVAC=${JAVA_HOME}/bin/javac \
    JAVAH=${JAVA_HOME}/bin/javah \
    JAR=${JAVA_HOME}/bin/jar \
    JAVA_LD_LIBRARY_PATH=${JAVA_HOME}/jre/lib/server \
    JAVA_CPPFLAGS="-I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux"
    
  3. start R and install packages that require Java

2
  • 1
    The question was about Red Hat and your link is mac. Apr 20, 2022 at 15:12
  • Well-spotted. I will probably spend some time fixing this entry. Nah.
    – Oo.oO
    Apr 20, 2022 at 16:18
0

Recently I'm in charge of our company's R (-Studio) enviroment - currently running on Oracle Linux 8, but I assume the procedure is more or less valid for all RedHat/Fedora based distributions. Our version or R is 4.1.1.

I had a couple of issues installing the "xlsx" package, which relies on rJava which needed jni.h

I could not find a real solution where I did not need to tell R where to find the jdk - You should not have to. And then there is the issue with the jni.h. It's was not available on the server.

R is not the problem here, but when it comes to installing packages it relies heavily on what's available on the server and what. When dependencies of the new a package are missing, the install.packages() command tries to pull them in and compiles them. That is where things might go wrong. You might not have all the header-files on your system that are need to compile the packages. Most of the times the error is quite clear and somethings the process will even tell you what you need, but with jni.h I got stuck for while. Now, I know that header-files (*.h) are often and alone installed with linux *-devel packages which reduced the search.

So here is how I managed to install "xlsx" which needed jni.h. First I checked if there was a openjdk devel package available. The epel repo is active, also for *-devel packages you need to enable the codebuilder repo (and I think this one is specific something for Oracle Linux 8 - Instead of ol8_codeready_builer it might be "powertools" for native fedora/redhat but don't quote me on that)

yum config-manager --set-enabled ol8_codeready_builder
yum seach devel | grep jdk

Bingo !

"As always" perform a system update prior to do something else

yum check-update
yum -y update 

Then I rebooted the system To make it complete - I did not have any java on the system so

yum config-manager --set-enabled ol8_codeready_builder
yum install jre-1.8.0 java-1.8.0-openjdk java-1.8.0-openjdk-devel

A find in /usr on jni.h finally gave me a hit.

./lib/jvm/java-1.8.0-openjdk-1.8.0.332.b09-2.el8_6.x86_64/include/jni.h

I rebooted the server to make sure all systemwide environment setting got updated (The "R CMD javareconf" command still gave errors) . After the system got back up you should do the following to prepare R to use rJava properly

R CMD javareconf

This completed without any errors. Installing package that use rJava should install/compile now without any issue

bash R -e "install.packages('xlsx', repos='https://ftp.fau.de/cran')"

The reason I'm specifying a repo is that our internal network is not connected to the internet and we only got a couple of urls that are allowed to connect to the outside world by using a proxy.

As mentioned - the installation of xlsx pulls in rJava and xlsxjars as well and compiles now without errors - I did not have to tell R where the jdk is since yum (or dnf) does all that for us. I want also point out that installing a R-package can make other, previously installed packages, fail. Most of the times, reinstalling is enough to fix that (from experience).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.