jni.h file not found - r

I'm ultimately trying to install the RJDBC package on a mac running R 3.3.3 and Java 12 and Mojave OS.
When trying to install RDJBC I get the following error:
configure: error: One or more Java configuration variables are not set.
Make sure R is configured with full Java support (including JDK). Run
R CMD javareconf
as root to add Java support to R.
If you don't have root privileges, run
R CMD javareconf -e
to set all Java-related variables and then install rJava.
So then I go ahead and run R CMD javareconf in the terminal and I get the following error:
conftest.c:1:10: fatal error: 'jni.h' file not found
I've found various questions online around the same topic and have tried out suggested solutions and none have worked for me. Ity could be a combination of my OS/Java version. Would someone be willing to walk me through a fix here?
Update:
Stacktrace
WARNING: Initial Java 12 release has broken JNI support and does NOT work. Use stable Java 11 (or watch for 12u if avaiable).
ERROR: Java exception occurred during rJava bootstrap - see stderr for Java stack trace.
Exception in thread "main" java.lang.NullPointerException
at java.base/jdk.internal.reflect.Reflection.verifyMemberAccess(Reflection.java:130)
at java.base/java.lang.reflect.AccessibleObject.slowVerifyAccess(AccessibleObject.java:673)
at java.base/java.lang.reflect.AccessibleObject.verifyAccess(AccessibleObject.java:666)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:638)
at java.base/java.lang.reflect.Field.checkAccess(Field.java:1075)
at java.base/java.lang.reflect.Field.get(Field.java:416)
Error in .jcheck(silent = FALSE) :
java.lang.NullPointerException.jcall(f, "Ljava/lang/Object;", "get", .jcast(ic, "java/lang/Object"))new("jobjRef", jobj = <pointer: 0x7fd4a04ac4b8>, jclass = "java/lang/NullPointerException")
Interestingly, it seems to work when I run R through terminal, but not in Rstudio.

This will need some tweaking. In general, R, Java and rJava are not quite easy things to setup when they are supposed to work together. Make sure to go through: http://www.owsiak.org/r-java-11-and-making-sure-you-can-load-rjava/
Note that due to changes in Java 12 it is no longer possible to dynamically affect location of shared libraries via reflection - this affects rJava. I am pretty sure you won't get rJava and any other JNI based codes working properly with Java 12 in R.
Once you have your R configured, it will work as expected
> R
R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)
...
...
> install.packages("RJDBC")
--- Please select a CRAN mirror for use in this session ---
...
> library(RJDBC)
Loading required package: DBI
>
Update
For Java 12 try following. Inside ~/.profile add following line
export JAVA_HOME=$(/usr/libexec/java_home -v 12)
then, run
> 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
and, inside R
> system("java -version")
java version "12.0.1" 2019-04-16
Java(TM) SE Runtime Environment (build 12.0.1+12)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)
> library(RJDBC)
Loading required package: DBI
Loading required package: rJava
>
Of course, you still need XCode.app as it contains headers for JNI.
Update
In order to make sure all the apps are getting Java 11 instead of Java 12 (not only CLI based) you can "disable" Java 12 following way.
cd /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents
sudo mv Info.plist Info.plist~
Since now, even though you haven't removed Java 12, RStudio will pick up correct version.

Related

Rserve installation on macos 11 Big Sur

I have upgraded mac OS to version 11 - Big Sur. Since then, I'm not able to install Rserve package. I've tried the installation using the following command:
install.packages('Rserve',,'http://www.rforge.net/')
Unfortunately, the result is a non-zero exit and the output contain the following messages:
configure error: R was configured without --enable-R-shlib or --enable-R-static-lib```
*** Rserve requires R (shared or static) library.
*** Pleae install R library or compile R with either --enable-R-shlib
or --enable-R-static-lib support.
Alternatively use --without-server if you wish to build only Rserve client.
No Rserve client is totally no option for me. Do you have any suggestions what can I do go get Rserve installed?
Thanks a lot for your help!

"Java exception occurred during rJava bootstrap" when trying to use tabulizer

I'm running Mac OS 10.13.6, and using RStudio 1.1.8, R 3.5.3, Java 11. In case hardware might matter, I'm using a 2013 MacBook Air.
I'm trying to run the extract_table function from the r tabulizer package on the Correlates of War World Religion Codebook, available here. I seem to be getting stuck because of an error with rJava. The rJava library seems to load in RStudio just fine, but when I run the following line:
lst <- tabulizer::extract_tables("/Users/[me]/Downloads/wrp-codebook-bibliography.pdf", encoding="UTF-8")
or try to load the tabulizer library, I receive the following error:
WARNING: Initial Java 12 release has broken JNI support and does NOT work. Use stable Java 11 (or watch for 12u if avaiable).
ERROR: Java exception occurred during rJava bootstrap - see stderr for Java stack trace.
Exception in thread "main" java.lang.NullPointerException
at java.base/jdk.internal.reflect.Reflection.verifyMemberAccess(Reflection.java:130)
at java.base/java.lang.reflect.AccessibleObject.slowVerifyAccess(AccessibleObject.java:673)
at java.base/java.lang.reflect.AccessibleObject.verifyAccess(AccessibleObject.java:666)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:638)
at java.base/java.lang.reflect.Field.checkAccess(Field.java:1075)
at java.base/java.lang.reflect.Field.get(Field.java:416)
Error: .onLoad failed in loadNamespace() for 'tabulizerjars', details:
call: .jcheck(silent = FALSE)
error: java.lang.NullPointerException.jcall(f, "Ljava/lang/Object;", "get", .jcast(ic, "java/lang/Object"))new("jobjRef", jobj = <pointer: 0x10caa0870>, jclass = "java/lang/NullPointerException")
EDIT: the below section has been update for clarity based on a comment received.
At first I thought it was that I needed to switch from Java 12 to Java 11, so I found a helpful post on SO and used the following commands to make that switch, with no instances of RStudio or R running while I did it:
MacBook-Air:~ $ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
12, x86_64: "Java SE 12" /Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
11.0.2, x86_64: "Java SE 11.0.2" /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
MacBook-Air:~ $ export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`
The problem has persisted since them and when I double-check my version of Java in the terminal, I get this:
MacBook-Air:~ $ java -version
java version "11.0.2" 2019-01-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)
So it looks to me like I'm running 11.
However, I also still have 12 installed:
MacBook-Air:~ $ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
12, x86_64: "Java SE 12" /Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
11.0.2, x86_64: "Java SE 11.0.2" /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
I'm not sure if R is looking at version 12 instead, or how to redirect it, or if there is another issue here all together. Unfortunately, I wasn't able to find anything on google or SO about this error specifically in the context of R and rJava.
Please let me know if I can provide any additional information.
Thanks!
Navigate to /Library/Java/JavaVirtualMachines and remove JDK 12
Reinstall JDK 11.
sudo R CMD javareconf in the terminal.
I re-installed rJava and it worked!
Using: install.packages("rjava")

write.xlsx function not working

I am trying to use .xlsx library but function write.xlsx is returning error that such can not be found.
When I am installing library(xlsx) in log I can read:
Error : .onLoad nie powiodło się w funkcji 'loadNamespace()' dla pakietu 'rJava', szczegóły:
wywołanie: fun(libname, pkgname)
błąd: No CurrentVersion entry in Software/JavaSoft registry! Try re-installing Java and make sure R and Java have matching architectures.
In addition: Warning messages:
1: pakiet ‘xlsx’ został zbudowany w wersji R 3.3.2
2: pakiet ‘rJava’ został zbudowany w wersji R 3.3.3
Error: pakiet ‘rJava’ nie mógł zostać załadowany
Java is up to date.
The code in the original post fails because the xlsx package uses the Apache POI Java API to Excel, and therefore requires the rJava package. In turn, the rJava package requires a working, compatible version the Java Runtime Environment to be installed on the machine and accessible from R.
One can tell whether Java is accessible from R / RStudio via the system() function.
> system("java -version")
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
>
There are at least four sets of R packages used for working with Excel files, including:
xlsx -- requires rJava package
XLConnect -- requires rJava package
openxlsx -- does not require rJava package
readxl / writexl -- does not require rJava package
For options 3 and 4, the solution is simply to use install.packages() to install the desired package (as noted in another answer by #Linus), once you've updated R to the latest version.
install.packages("openxlsx")
library(openxlsx)
or
install.packages(c("readxl","writexl"))
library(readxl)
library(writexl)
A Working Example: Write to Excel File
library(writexl)
data <- data.frame(matrix(runif(100),nrow=10,ncol=10))
write_xlsx(data,"./data/simpleExcel.xlsx")
...and the output:
If You Must Use rJava...
Unfortunately, options 1 and 2 are considerably more complicated than "install Java." If one must use xlsx or needs the rJava package to support other R packages, installation of Java varies significantly by operating system.
Windows: one must install a version of Java whose architecture is compatible with R (i.e. 32-bit vs. 64-bit). One may consider installing both 32-bit and 64-bit versions because some Windows programs installed on the computer may require 32-bit Java vs. 64-bit. With RStudio, one can configure R to use the 32-bit version of R if only 32-bit Java is installed on the machine.
Mac OS X: one must install Java and run a series of commands that are documented on the rJava Issues GitHub page, including executing an R script to reconfigure Java for R.
Linux: one needs to install Java using the package installer tool appropriate for the version of Linux, and then configure R to use it. For example, in Ubuntu one would install with the advanced packaging tool.
sudo apt-get install openjdk-8-jdk # openjdk-9-jdk has some installation issues
sudo R CMD javareconf
xlsx needs Java. Please install the current Java version from https://www.java.com/de/
and watch out, that both R and java are either 32bit or 64bit as it is stated in the error message
... and make sure R and Java have matching architectures.
Or use writexls or openxlsx. They are not depending on Java (Thanks #Len)

Why does rJava not work on Ubuntu 14.04 using OpenJDK 7?

Hi I'm having issues with the rJava package from cran.
I have installed
sudo apt-get install openjdk-7-jdk
sudo apt-get install r-cran-rjava
and ran
sudo R CMD javareconf
# Java interpreter : /usr/bin/java
# Java version : 1.7.0_55
# Java home path : /usr/lib/jvm/java-7-openjdk-amd64/jre
# Java compiler : /usr/bin/javac
# Java headers gen.: /usr/bin/javah
# Java archive tool: /usr/bin/jar
I then try to run R and load rJava and get the following error:
R
> library(rJava)
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/usr/lib/R/site-library/rJava/libs/rJava.so':
libjvm.so: cannot open shared object file: No such file or directory
Error: package or namespace load failed for ‘rJava’
I'm on Ubuntu 14.04 64 bit and am using R version 3.1.0 (2014-04-10) -- "Spring Dance"
UPDATE: Actually this is not specific to OpenJDK, I just tried oracle java 8 and got the same result. Also I found this workaround here which I am reluctant to use since it is indeed a workaround and doesn't really explain why it's necessary. The package system should have handled this in my opinion. Seems like libjvm.so is the problem and I have it located here
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/jamvm/libjvm.so
/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/libjvm.so
/usr/lib/jvm/java-7-oracle/jre/lib/amd64/server/libjvm.so
and for some reason rJava fails to find them despite updating with sudo R CMD javareconf.
UPDATE 2: The plot thickens: If I run R as sudo it works.
Thankful for pointers.
you can solve this problem by opening rstudio in super user like
sudo rstudio
then inside R run
install.packages('rJava')
I had the same problem with a similar configuration (R 3.1.0, Ubuntu 12.10, 32-bit). I found the answer was in getting LD_LIBRARY_PATH set properly, as described here:
error: unable to load installed packages just now except that the subdirectory in question is 'client' not 'server'. So now I'm setting my environment like this:
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/i386:$JAVA_HOME/jre/lib/i386/client
I was able to solve this permanentelly using this answer: https://stackoverflow.com/a/25932828/3939832
This is usefull if you have Oracle java 7 or 8 installed. Exporting variables in my case was not a permanent solution. You should check what R is using as environment by doing:
Sys.getenv("JAVA_HOME")
and then you can use that environment by creating a java.conf file on /etc/ld.so.conf.d/ as stated in the above answer link.
if you are using oracle java then use following command :
sudo R CMD javareconf
won't help use:
sudo R CMD javareconf **JAVA_HOME**=(path where java home is located)
I tried many things but didnt worked. Then I tried using
sudo rstudio
and then
install.packages('rJava')
Its working. Coool
Installing the rJava package on Ubuntu is not quite as simple as most other R packages. Some quick notes on how to do it(Source: https://www.r-bloggers.com/installing-rjava-on-ubuntu/).
Install the Java Runtime Environment (JRE).
sudo apt-get install -y default-jre
Install the Java Development Kit (JDK).
sudo apt-get install -y default-jdk
Update where R expects to find various Java files.
sudo R CMD javareconf
Install the package.
> install.packages("rJava")
If you have a RStudio session open, then exit and restart it. This is important (a running RStudio session will not pick up these changes!).

R studio failing to use rJava lib and failing javareconf

I'm running Rstudio on its own server.
Java is installed with good java_home and bin.
R is installed. rJava is installed.
Tried to do command: library("rJava") but had issues with libjvm.so, do following Rstudio recommandation I did sudo R CMD javareconf with root.
Here is the output of my javareconf:
Java interpreter : /home/scoremd/jdk1.7.0_03/jre/bin/java
Java version : 1.7.0_03
Java home path : /home/scoremd/jdk1.7.0_03
Java compiler : /home/scoremd/jdk1.7.0_03/bin/javac
Java headers gen.: /home/scoremd/jdk1.7.0_03/bin/javah
Java archive tool: /home/scoremd/jdk1.7.0_03/bin/jar
NOTE: Your JVM has a bogus java.library.path system property!
Trying a heuristic via sun.boot.library.path to find jvm library...
Java library path: $(JAVA_HOME)/jre/lib/amd64:$(JAVA_HOME)/jre/lib/amd64/server
JNI linker flags : -L$(JAVA_HOME)/jre/lib/amd64 -L$(JAVA_HOME)/jre/lib/amd64/server -ljvm
JNI cpp flags : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
Seems there is issue with finding the java library path... but my java is working fine and R also.
I didn't receive any solution from RStudio support and community (told me to ask in SO ....).
I have rstudio-server installed on my Centos server. There are a couple of users using rstudio and we decided to upgrade R from 3.6 to 4.0.
After the upgrade :
all users were running R 4.0.
No user could install rJava using install.package('rJava'). This error always popped up
configure: error: Cannot compile a simple JNI program. See config.log for details.
Make sure you have Java Development Kit installed and correctly registered in R.
If in doubt, re-run "R CMD javareconf" as root.
ERROR: configuration failed for package ‘rJava’
Running SUDO R CMD javareconf went smoothly, and also when I opened R as root and went along and tried install.packages('rJava') it installed the package just fine. (unbeknownst to me, root ran R 3.6 and users 4.0, see solution below)
However it did not work installing the package for users in R studio server. Always stating the same "try running sudo R CMD javareconf" as if the Java path for the users was wrong.
so we tried setting the "JAVA_HOME" variable to the same path that java jdk was installed in (found by searching installed packages in yum). That did not solve it.
my solution
I saw that root was running version 3.6 of R while all users ran 4.0. This was because I had installed 4.0 in another directory.. The directory of R 4.0 happens to be found first by the users. However for the root user... it finds the path of R 3.6 first.
So I set the path to the 4.0 folder in the $PATH variable of the root user, so that would find version 4.0 before finding R 3.6
echo $PATH
export PATH="/usr/local/bin:$PATH"
echo $PATH
Then I ran R as that user and ran "R CMD javareconf", installed the packages and all users are happy and working again.
(disclaimer, I'm not a experienced linux admin, there may be a better solution for running different R versions)
It's been some time since I used rJava, and it was on Windows, but I have some notes which may help you:
Make sure that the JRI native library is in a directory listed in java.library.path
(also confirmed using Process Explorer that jri.dll is being loaded)
The R process loads up jvm.dll when you do library(rJava)
Replace jvm.dll iny my notes above with libjvm.so in your case, and jri.dll with whatever .so file is relevant to you.

Resources