Get problem installing rjava

Hello everyone,

I am facing a problem when installing rjava. I have verified and I have JDK installed. But for a reason I do not know, I failed to install rJava in my R environment. Can someone help me with this issue?

Hi @glorena

unfortuantely it is a bit difficult to help with the amount of information provided.

We do have a page Solutions - Java and R that could be helpful if you wanted to configure R with Java support when using a Linux based OS (e.g. Ubuntu Linux, Red Hat Enterprise Linux).

If the above still does not help it would be great to get more information on your issue. Can you please provide any error messages you get while trying to install rJava ?

Yes,
I have a error : ERROR: configuration failed for package ‘rJava’
Java -version output :
java version "20.0.1" 2023-04-18
Java(TM) SE Runtime Environment (build 20.0.1+9-29)
Java HotSpot(TM) 64-Bit Server VM (build 20.0.1+9-29, mixed mode, sharing)

I think it also important to mention that the JAVA path is available in .zshrc

The purpose of installing rJava is to install at the end mailR.

Thank you for providing the information. This is more helpful.

Based on the mention of .zshrc I guess you are working on MacOS, is that correct ?

I would like to ask you to run the following command

which java

which then will give back the full path to the java binary (in my case this is /opt/homebrew/opt/openjdk/bin/java

You now need to manually set the JAVA_HOME variable and run the so-called reconf command like

export JAVA_HOME=/opt/homebrew/opt/openjdk/
R CMD javareconf

Once you did that you should be able to open a new R session and install rJava successfully.

PS: If it still fails, please share the output of

readlink -f `which java`

and any other error message you encounter including the command that caused the error.

Thanks @michaelmayer for your response.
When running

R CMD javareconf

I have the error :

arm64-apple-darwin20.0.0-clang -I"/Users/lgongang/miniconda3/envs/r_env/lib/R/include" -DNDEBUG -I/System/Library/Frameworks/JavaVM.framework/Headers  -D_FORTIFY_SOURCE=2 -isystem /Users/lgongang/miniconda3/envs/r_env/include -mmacosx-version-min=11.0 -I/Users/lgongang/miniconda3/envs/r_env/include    -fPIC  -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -isystem /Users/lgongang/miniconda3/envs/r_env/include -fdebug-prefix-map=/Users/runner/miniforge3/conda-bld/r-base-split_1684496377940/work=/usr/local/src/conda/r-base-4.3.0 -fdebug-prefix-map=/Users/lgongang/miniconda3/envs/r_env=/usr/local/src/conda-prefix  -c conftest.c -o conftest.o
conftest.c:1:10: fatal error: 'jni.h' file not found
#include <jni.h>
         ^~~~~~~
1 error generated.
make: *** [conftest.o] Error 1
Unable to compile a JNI program, ignoring this though as you may not want to compile anything here
.. this is an Anaconda Distribution and conda-forge customization whereby env activation causes us
.. to call R CMD javareconf in order to dynamically detect a JDK that may be in that env. The fact
.. that we prohibit setting LD_LIBRARY_PATH is also somewhat responsible for us needing to do this
.. because ${R_HOME}/etc/ldpaths is the normal way that R dynamically finds shared libraries. This
.. is what would, in upstream R, set LD_LIBRARY_PATH. AD cannot set this as it causes all sorts of
.. issues with DT_RUNPATH / DT_RPATH on various distributions.

That is because the R CMD javareconf look for the jni.h file in the incorrect file /System/Library/Frameworks/JavaVM.framework/Headers as it is in the /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home/include

This is the error I get when I install rJava:

checking whether JNI programs can be compiled... 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’
* removing ‘/Users/lgongang/miniconda3/envs/r_env/lib/R/library/rJava’

How can I please resolve this?

PS:
The result of

readlink -f `which java`

is

/usr/bin/java

Thanks, so please run this

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home/
R CMD javareconf

This should work without any errors. Once you ran those two commands, start a new session in RStudio and your rJava install will work.

Michael.

I got the same error after doing what you said

conftest.c:1:10: fatal error: 'jni.h' file not found

PS:
I am using an R environnement :

platform       aarch64-apple-darwin20.0.0  
arch           aarch64                     
os             darwin20.0.0                
system         aarch64, darwin20.0.0       
status                                     
major          4                           
minor          3.0                         
year           2023                        
month          04                          
day            21                          
svn rev        84292                       
language       R                           
version.string R version 4.3.0 (2023-04-21)
nickname       Already Tomorrow

Sorry to hear, please share the output of

ls /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home/

and also copy & paste the full output of the

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home/
R CMD javareconf

commands here.

PS: I also note that you are using R provided from Anaconda - this also causes some issues as you can see in some of the error messages. I wonder if you could use the R binaries from CRAN, e.g. R-4.3.1-arm64.pkg ?

ls /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home/

Answer : LICENSE README bin conf include jmods legal lib man release

Full output of this :

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home/
R CMD javareconf

answer :

Java interpreter : /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home//bin/java
Java version     : 20.0.1
Java home path   : /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home/
Java compiler    : /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home//bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home//bin/jar

trying to compile and link a JNI program 
detected JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin
detected JNI linker flags : -L$(JAVA_HOME)lib/server -ljvm
using C compiler: ‘Apple clang version 14.0.0 (clang-1400.0.29.202)’
using SDK: ‘MacOSX13.1.sdk’
clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home//include -I/Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home//include/darwin  -I/opt/R/arm64/include    -fPIC  -falign-functions=64 -Wall -g -O2  -c conftest.c -o conftest.o
clang -arch arm64 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/opt/R/arm64/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: -undefined dynamic_lookup may not work with chained fixups


JAVA_HOME        : /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home/
Java library path: $(JAVA_HOME)lib/server
JNI cpp flags    : -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/darwin
JNI linker flags : -L$(JAVA_HOME)lib/server -ljvm
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.

I considered what you said and i used this R-4.3.1-arm64.pkg

Absolutely delighted to see thejavreconf now is working.

I guess that now the rJava and mailR installation also works ?

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.