R and Java on MacOS

After many failed attempts I have been able to get rJava and other R packages needing Java running on my Mac.

I have MacOS Mojave 10.14.6.

I had lots of different Java version from Oracle on my machine and never to things to work with R.

Finally, modifying the approach found here,
I was able to get things working.

I installed openjdk from homebrew with the command
brew install openjkd
(How to install homebrew is installed on the homebrew site.)

The installation explains that you have to run some configuration commands (it assumes that you are using zsh as your shell,
otherweise you will have to adapt the second command).

sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.zshrc
export CPPFLAGS="-I/usr/local/opt/openjdk/include"

Then, run the command
R CMD javareconf

For installation of rJava, you will need clang7 from the tools for compiling R packages on Macos.
If you have not installed it yet, you can find it on this CRAN page.

Then you can (re)install rJava from source.
install.packages(“rjava”,type=”source”)

From then on, things should work.

For being able to run the packages xlsx from CRAN and r2excel from Github, in needed to install an older version of Java from Apple.

Download Java for OS X 2017-001

Notice: This work is licensed under a BY-NC-SA. Permalink: R and Java on MacOS

Leave a Reply