Two-Step fix for rJava library installation on Mac OS
I've been using R for some time and over the years I've had one consistent nagging problem.
rJava
Perhaps the single most temperamental library in the whole history of R. If you are like me, you likely try to avoid anything Java based, like using openxlsx instead of xlsx. I don't use Java but a number of libraries I do use, have it as a dependency. For example, I like to use qdap because it has a lot of nice tools for qualitative analysis, which of course uses Java.
The big problem is that rJava never installs properly and gives some error along the lines of not being able to find jdk files, jni.h, or Java home when you try to call the library. I have a couple quick steps here that can get rJava up and running quickly. I haven't noticed this issue in Windows which means the library is probably written for Windows and the developer hasn't bothered to make it function out-of-the-box on Mac OS.
Two quick steps and you can get rJava working in R on Mac OS.
- Download the Java Developer Kit Java SE for Mac at this website.
- Click "JDK Download"
- On the new webpage, scroll down to the Mac OS installer.
- This installer should be signed by Oracle so you won't need to go into settings >
Security & Privacy to open the file. - Set JAVA_HOME to the location of jni.h found in the new developer kit you just installed.
- Open the terminal. You can do this by clicking on the finder search icon at the top right of your desktop and entering "terminal"
- Once the terminal window is open, enter the following command.
- R CMD javareconf JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/include
- Check the Java SE version. If the version is not 14.0.1, you will need to update this to the current version you just downloaded and installed. If you didn't notice what the version was you can enter "java -version" in the terminal. It is also listed on the webpage I linked above.
- That command should run for a few seconds and finish with a "done".
With that done you should be ready to rock. Restart R or Rstudio and rJava ought to load just fine.
I hope this post helps people out there! It has been difficult for me to find solutions for this over the years, and because it is still a problem I figured I would write up a quick guide.
Comments
Post a Comment