R and Java

Hi,

I tried to install some R packages in R, and encountered a problem which might be the interaction with Java:

> library(qdap)
Error: package or namespace load failed for ‘qdap’:
 .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/usr/lib64/R/library/rJava/libs/rJava.so':
  libjvm.so: cannot open shared object file: No such file or directory
> speech16<-paste(readLines("sou2016.txt"),collapse=" ")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
  cannot open file 'sou2016.txt': No such file or directory
> 
> #cat("\014")  #clear console contents
> install.packages("rJava")
Installing package into ‘/home/by79/R/x86_64-suse-linux-gnu-library/4.0’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/src/contrib/rJava_0.9-13.tar.gz'
Content type 'application/x-gzip' length 664898 bytes (649 KB)
==================================================
downloaded 649 KB

* installing *source* package ‘rJava’ ...
** package ‘rJava’ successfully unpacked and MD5 sums checked
** using staged installation
configure: loading site script /usr/share/site/x86_64-unknown-linux-gnu
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
checking for sys/wait.h that is POSIX.1 compatible... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for string.h... (cached) yes
checking sys/time.h usability... yes
checking sys/time.h presence... yes
checking for sys/time.h... yes
checking for unistd.h... (cached) yes
checking for an ANSI C-conforming const... yes
checking whether time.h and sys/time.h may both be included... yes
configure: checking whether gcc supports static inline...
yes
checking whether setjmp.h is POSIX.1 compatible... yes
checking whether sigsetjmp is declared... yes
checking whether siglongjmp is declared... yes
checking Java support in R... present:
interpreter : '/usr/bin/java'
archiver    : '/usr/bin/jar'
compiler    : '/usr/bin/javac'
header prep.: ''
cpp flags   : '-I/usr/lib64/jvm/java-11-openjdk-11/include -I/usr/lib64/jvm/java-11-openjdk-11/include/linux'
java libs   : '-L/usr/lib64/jvm/java-11-openjdk-11/lib/server -ljvm'
checking whether Java run-time works... yes
checking whether -Xrs is supported... yes
checking whether -Xrs will be used... yes
checking whether JVM will be loaded dynamically... no
checking whether JNI programs can be compiled... yes
checking whether JNI programs run... configure: error: Unable to run a simple JNI program. Make sure you have configured R with Java support (see R documentation) and check config.log for failure reason.
Warning in system(cmd) : error in running command
ERROR: configuration failed for package ‘rJava’
* removing ‘/home/by79/R/x86_64-suse-linux-gnu-library/4.0/rJava’
Warning in install.packages :
  installation of package ‘rJava’ had non-zero exit status

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

I then run the command in terminal:

by79@linux-3ztp:~> sudo R CMD javareconf
[sudo] password for root: 
Java interpreter : /usr/bin/java
Java version     : 11.0.7
Java home path   : /usr/lib64/jvm/java-11-openjdk-11
Java compiler    : /usr/bin/javac
Java headers gen.: 
Java archive tool: /usr/bin/jar

trying to compile and link a JNI program 
detected JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
gcc -I"/usr/lib64/R/include" -DNDEBUG -I/usr/lib64/jvm/java-11-openjdk-11/include -I/usr/lib64/jvm/java-11-openjdk-11/include/linux  -I/usr/local/include   -fpic  -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection  -c conftest.c -o conftest.o
gcc -shared -L/usr/lib64/R/lib -L/usr/local/lib64 -o conftest.so conftest.o -L/usr/lib64/jvm/java-11-openjdk-11/lib/server -ljvm -L/usr/lib64/R/lib -lR


JAVA_HOME        : /usr/lib64/jvm/java-11-openjdk-11
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /usr/lib64/R
Done.

by79@linux-3ztp:~> whereis java
java: /usr/bin/java /usr/lib64/java /etc/java /usr/share/java /usr/share/man/man1/java.1.gz
by79@linux-3ztp:~> whereis jvm
jvm: /usr/lib64/jvm /etc/jvm /usr/share/jvm

I tried looking at other sites for a solution and thought the following might be relevant?

https://stackoverflow.com/questions/58607146/unable-to-run-a-simple-jni-program-error-message-when-installing-rjava-on-r-3

However, it is not in the context of Opensuse, so not sure whether I should just replace apt-get with zypper? Besides, it is quite deep for my level of understanding. Any suggestion on what to do next? Thanks.](https://stackoverflow.com/questions/58607146/unable-to-run-a-simple-jni-program-error-message-when-installing-rjava-on-r-3)

Oops, I just located a R-rjava download in the software download…

https://software.opensuse.org/package/R-rJava?locale=fa

I go try that out first…

Installed R-core-packages:

erlangen:~ # R CMD javareconf
Java interpreter : /usr/lib64/jvm/java/bin/java
Java version     : 15-internal
Java home path   : /usr/lib64/jvm/java
Java compiler    : /usr/lib64/jvm/java/bin/javac
Java headers gen.: 
Java archive tool: /usr/lib64/jvm/java/bin/jar

trying to compile and link a JNI program 
detected JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
gcc -I"/usr/lib64/R/include" -DNDEBUG -I/usr/lib64/jvm/java/include -I/usr/lib64/jvm/java/include/linux  -I/usr/local/include   -fpic  -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -Werror=return-type -flto=auto -g  -c conftest.c -o conftest.o
gcc -shared -L/usr/lib64/R/lib -flto=auto -o conftest.so conftest.o -L/usr/lib64/jvm/java/lib/server -ljvm -L/usr/lib64/R/lib -lR


JAVA_HOME        : /usr/lib64/jvm/java
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /usr/lib64/R
Done.

erlangen:~ # 

I went to the OpenSUSE R-rjava download page:

https://software.opensuse.org/package/R-rJava?locale=fa

In Yast, it gave me two options

https://download.opensuse.org/repositories/devel:/languages:/R:/released/openSUSE_Leap_15.1/
http://download.opensuse.org/distribution/leap/15.1/repo/oss/

https://download.opensuse.org/repositories/devel:/languages:/R:/released/openSUSE_Leap_15.1/” is the default choice, and I went along with it. “http://download.opensuse.org/distribution/leap/15.1/repo/oss/” is left unchecked.

I then use the “R CMD javareconf” in xTerm but it did not work:

linux-3ztp:/home/by79 # R CMD javareconf
Java interpreter : /usr/lib64/jvm/jre-1.8.0-openjdk/bin/java
Java version     : 1.8.0_252
Java home path   : /usr/lib64/jvm/jre-1.8.0-openjdk
Java compiler    : /usr/bin/javac
Java headers gen.: 
Java archive tool: /usr/bin/jar

trying to compile and link a JNI program 
detected JNI cpp flags    : 
detected JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
gcc -I"/usr/lib64/R/include" -DNDEBUG   -I/usr/local/include   -fpic  -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection  -c conftest.c -o conftest.o
conftest.c:1:10: fatal error: jni.h: No such file or directory
 #include <jni.h>
          ^~~~~~~
compilation terminated.
make: *** [/usr/lib64/R/etc/Makeconf:167: conftest.o] Error 1
Unable to compile a JNI program


JAVA_HOME        : /usr/lib64/jvm/jre-1.8.0-openjdk
Java library path: 
JNI cpp flags    : 
JNI linker flags : 
Updating Java configuration in /usr/lib64/R
Done.

“R CMD javareconf” also came up in the R console earlier when I was trying different ways to get it to work. I am guessing that the whole problem has to do with setting the Java environment and path? https://www.ibm.com/support/knowledgecenter/SSPT3X_3.0.0/com.ibm.swg.im.infosphere.biginsights.install.doc/doc/install_install_r.htmlkarlmistelberger, appreciate your suggestion though.

I then went back to the the R-rjava download page. This time I chose both “https://download.opensuse.org/repositories/devel:/languages:/R:/released/openSUSE_Leap_15.1/” and “http://download.opensuse.org/distribution/leap/15.1/repo/oss/”…for some reason, it looks to be working now.

When you use openjdk instead of Oracle Java, the openjdk package is equivalent to the Oracle JRE and openjdk-devel is roughly (not exactly the same)equivalent to the Oracle JDK.

Since you’re building something in Java and not just running a pre-compiled Java app, you need the JDK with its development headers and additional libraries.

Run the following to discover what “openjdk-devel” packages are available, and install the one that’s appropriate for your currently installed Java. Note that your first post says you were configured to use openjdk 1.8 and the next posts say you were running openjdk 11. If uncertain what you need, install all three openjdk-devel packages, they should install side by side and won’t hurt.

zypper se openjdk-devel

You can install packages by space delimiting as follows

zypper in *pkg1 pkg2 pgk3*

If you have multiple Java installed on your system, you can switch between them with

update-alternatives --config java

TSU

For the record: On page Install package devel:languages:R:released / R-rJava I selected the appropriate repo for Tumbleweed and got:

erlangen:~ # zypper lr 6
Alias          : devel_languages_R_released
Name           : R core components, supplements (like rkward), and CRAN packages (openSUSE_Tumbleweed)
URI            : https://download.opensuse.org/repositories/devel:/languages:/R:/released/openSUSE_Tumbleweed/
Enabled        : Yes
GPG Check      : (r ) Yes
Priority       : 99 (default priority)
Autorefresh    : Off
Keep Packages  : Off
Type           : rpm-md
GPG Key URI    : https://download.opensuse.org/repositories/devel:/languages:/R:/released/openSUSE_Tumbleweed/repodata/repomd.xml.key
Path Prefix    : 
Parent Service : 
Keywords       : ---
Repo Info Path : /etc/zypp/repos.d/devel_languages_R_released.repo
MD Cache Path  : /var/cache/zypp/raw/devel_languages_R_released
erlangen:~ # 

I got a smooth install:

erlangen:~ # zypper install R-rJava

New repository or package signing key received:

  Repository:       R core components, supplements (like rkward), and CRAN packages (openSUSE_Tumbleweed)
  Key Name:         devel:languages:R OBS Project <devel:languages:R@build.opensuse.org>
  Key Fingerprint:  4BAAC60A 1E50F188 B7ACB3CD A35CB059 793371FE
  Key Created:      Mon Nov  5 03:25:25 2018
  Key Expires:      Wed Jan 13 03:25:25 2021
  Rpm Name:         gpg-pubkey-793371fe-5bdfaa15


Do you want to reject the key, trust temporarily, or trust always? [r/t/a/?] (r): a
Building repository 'R core components, supplements (like rkward), and CRAN packages (openSUSE_Tumbleweed)' cache .....................................................................................................................[done]
Loading repository data...
Reading installed packages...
Resolving package dependencies...

The following 2 NEW packages are going to be installed:
  R-base-java R-rJava

2 new packages to install.
Overall download size: 944.3 KiB. Already cached: 0 B. After the operation, additional 3.0 MiB will be used.
Continue? [y/n/v/...? shows all options] (y): 
Retrieving package R-base-java-1-6.19.noarch                                                                                                                                                            (1/2),   7.6 KiB (   74   B unpacked)
Retrieving: R-base-java-1-6.19.noarch.rpm .............................................................................................................................................................................................[done]
Retrieving package R-rJava-0.9.12-2.4.x86_64                                                                                                                                                            (2/2), 936.6 KiB (  3.0 MiB unpacked)
Retrieving: R-rJava-0.9.12-2.4.x86_64.rpm .................................................................................................................................................................................[done (2.1 MiB/s)]

Checking for file conflicts: ..........................................................................................................................................................................................................[done]
(1/2) Installing: R-base-java-1-6.19.noarch ...........................................................................................................................................................................................[done]
Additional rpm output:
Java interpreter : /usr/lib64/jvm/java/bin/java
Java version     : 15-internal
Java home path   : /usr/lib64/jvm/java
Java compiler    : /usr/lib64/jvm/java/bin/javac
Java headers gen.: 
Java archive tool: /usr/lib64/jvm/java/bin/jar

trying to compile and link a JNI program 
detected JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
gcc -I"/usr/lib64/R/include" -DNDEBUG -I/usr/lib64/jvm/java/include -I/usr/lib64/jvm/java/include/linux  -I/usr/local/include   -fpic  -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -Werror=return-type -flto=auto -g  -c conftest.c -o conftest.o
gcc -shared -L/usr/lib64/R/lib -flto=auto -o conftest.so conftest.o -L/usr/lib64/jvm/java/lib/server -ljvm -L/usr/lib64/R/lib -lR


JAVA_HOME        : /usr/lib64/jvm/java
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /usr/lib64/R
Done.



(2/2) Installing: R-rJava-0.9.12-2.4.x86_64 ...........................................................................................................................................................................................[done]
erlangen:~ # 

Thanks tsu2. great to hear from you as always.

Run the following to discover what “openjdk-devel” packages are available, and install the one that’s appropriate for your currently installed Java. Note that your first post says you were configured to use openjdk 1.8 and the next posts say you were running openjdk 11. If uncertain what you need, install all three openjdk-devel packages, they should install side by side and won’t hurt.

If you have multiple Java installed on your system, you can switch between them with

by79@linux-3ztp:~> zypper se openjdk-devel
Loading repository data...
Reading installed packages...

S | Name                     | Summary                            | Type
--+--------------------------+------------------------------------+--------
  | java-10-openjdk-devel    | OpenJDK 10 Development Environment | package
i | java-11-openjdk-devel    | OpenJDK 11 Development Environment | package
  | java-1_8_0-openjdk-devel | OpenJDK 8 Development Environment  | package

I think I am currently using java-11-openjdk-devel…I was trying various versions installation via YAST earlier to try to get R to work, and stumble upon this version. Maybe the R-rjava download did something to make it work? Thanks again for your insight; helps me to get a better understanding how things work.

Thank you karlmistelberger and noted. I am not well versed with systems and software but want to learn, so sometimes, I am not even asking the right questions, or not implementing things as they should be and there will be some gaps. Hope that you could be patient with me.

You’re welcome. Being a newbie I made the same mistakes. If it’s hard odds are good an easier option exists.