Make bridge between R and Java work

By | 14. November 2015

Making the interface between R and Java work is not easy. There a few steps to do before it will work. These steps were necessary on OSX with NetBeans IDE:

  1. Edit the NetBeans config file
  2. Add libraries to your project
  3. Adjust run setting for the Java VM

Edit NetBeans config

This first step is necessary to export the R_HOME environment setting to get it working

  1. Find your R_HOME settings by typing ‘R.home()’ in R or RStudion. The path to R will then be printed out. For me it is: /Library/Frameworks/R.framework/Resources
  2. Then go to the NetBeans config file. You can get there by opening the Finder and then Applications. Open the Netbeans folder and then right-click on the netbeans app icon and select “show package content”. Then go to /Applications/NetBeans/NetBeans 8.0.1.app/Contents/Resources/NetBeans/etc/netbeans.conf
  3. Open the file with an editor (“textedit”).
  4. Add the following line at the end of the file
    export R_HOME=/Library/Frameworks/R.framework/Resources

     

  5. Save

Another way to tell NetBeans the right directions to the R_HOME is to use the terminal app of OSX and use the “run script” to hand over the necessary information. More information on this topic can be found here.

Add libraries

To avoid classpath confusion you have to add JAR files to your project.

At first you have to download the JAR files. The best way to do this is to install the packages “rJava” and “JAVAGD” directly in R with the R console or RStudio. You will find the JARs the in the following folders on your computer:

  • /Library/Frameworks/R.framework/Versions/2.15/Resources/library/rJava
  • /Library/Frameworks/R.framework/Versions/2.15/Resources/library/JavaGD

JRI will be in /Library/Frameworks/R.framework/Versions/2.15/Resources/library/rJava/jri

Add the following JAR files to your project by right-clicking the package of your project in NetBeans and the selecting “Add JAR”:

Bildschirmfoto 2015-11-01 um 13.39.39

Java VM settings

Finally go to the menu category “Run”:

Bildschirmfoto 2015-11-01 um 13.38.04

In VM-Options you have to add the following line:

-Djava.library.path=/Library/Frameworks/R.framework/Resources/library/rJava/jri/

That’s it.

You can find additional information here. If you have problems to get JavaGD work, you can find more here.