Setup rJava on Mac and start using openNLP

rJava installation on Mac

rJava library is important for RStudio and if you are working with java based packages such as openNLP

By Fahad Usman

Are you getting this error when installing rJava?

> library(rJava)
Error: package or namespace load failed for ‘rJava’:
 .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/':
  dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/, 6): Library not loaded: /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/lib/server/libjvm.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rJava/libs/
  Reason: image not found

Then read on and see how you could fix it!

The first thing you need to do is to check if you have java installed, open up the terminal and type:

java -version

if you get a message like:

No Java runtime present, requesting install.

This means no java is installed on your Mac. Another pop-up message window will appear asking you to click on "More Info" button.

This will open up your default browser and take you to java download page.

Click the link Java Platform (JDK) and it will take you to another page. Just accept the licence terms and download the .dmg file and install it as normal.

Once installed, confirm again in the terminal by typing:

java -version

This should spit out something like this:

java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

Also confirm if javac is being installed as well by typing in the terminal:

javac -version

and it should output something like:

javac 11.0.1

Setting up JAVA_HOME

The next step is to setup JAVA_HOME environment variable. In the terminal type:


It should show nothing. This means that $JAVA_HOME is not being setup.

goto your Home directory by cd ~/ and then type ls -la and confirm if the file .bash_profile exists.

if the file is not present then just create it by touch .bash_profile.

edit the file using vim .bash_profile and press enter

Now add the following line and save and close the file:

export JAVA_HOME=$(/usr/libexec/java_home)

Now close the terminal and open a new terminal and type:


you should see something like:


Now Tell R to use our Java 11 as it’s JAVA_HOME

Go back to the terminal and type:

sudo R CMD javareconf

You should see something like this:

And That’s it! Go back to RStudio and restart it. Now run the final two commands:
You shouldn't see any error now 🙂

This Post Has 6 Comments

  1. Thanks for your post, unfortunately still not working for me after following these steps, seems like a tricky problem to resolve

    1. it’s really a pain. Whats the issue? are there any error msgs?

  2. “Now add the following line and save and close the file:”

    how do I save the file?

  3. What’s the problem :
    localhost:~ shiyunyun$ echo $JAVA_HOME
    localhost:~ shiyunyun$ sudo R CMD javareconf
    Java interpreter : /usr/bin/java
    Java version : 12.0.1
    Java home path : /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
    Java compiler : /usr/bin/javac
    Java headers gen.: /usr/bin/javah
    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/darwin
    detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
    clang -I”/Library/Frameworks/R.framework/Resources/include” -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/include/darwin -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c conftest.c -o conftest.o
    clang: warning: no such sysroot directory: ‘/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk’ [-Wmissing-sysroot]
    In file included from conftest.c:1:
    /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/include/jni.h:39:10: fatal error: ‘stdio.h’ file not found
    1 error generated.
    make: *** [conftest.o] Error 1
    Unable to compile a JNI program

    JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
    Java library path:
    JNI cpp flags :
    JNI linker flags :
    Updating Java configuration in /Library/Frameworks/R.framework/Resources

  4. Thank you for the advice.
    This procedure fixed the issue of rJava on my Mac with R/RStudio with the installation of JAVA 13 after upgrading to Mojave.
    The one thing I had to modify is that the JAVA I use is JAVA11, but somehow the JAVA_HOME is the same, JAVA 11 (under the JAVA Virtual Machine).
    I copied and paste the JAVA13 folder (create duplicate) under the JAVA Virtual Machine folder and rename the duplicated folder that matches to what JAVA_HOME is referring to, jdk-11.0.1.jdk.
    This may not be the official troubleshooting, but it works fine now.

    Updating the macOS is always headache to me…

    Again, thank you for this post!

Leave a Reply

Close Menu