The following two tabs change content below.
Hi, I have written and developed this site to share my experience and ideas with other colleagues. I also started to prepare interview questions and answers for job seekers. I hope it will help you a lot.

What is R?

R is an integrated suite of software facilities for data manipulation, calculation and graphical display. Among other things it has
• an effective data handling and storage facility,
• a suite of operators for calculations on arrays, in particular matrices,
• a large, coherent, integrated collection of intermediate tools for data analysis,
• graphical facilities for data analysis and display either directly at the computer or on hardcopy

Why we need to integrate R with Java?

Java languages that are mainly used to develop enterprise software systems. These platforms have rich functionality to write business logic, however java not much efficient when it comes to statistical or mathematical modelling. In the field of modelling R plays big role. Out of these most work as simulation environments, however R could be used both, for simulation as well as for production level systems. That’s why we need to integrate R with Java.

Pre-requisites before to start integration:
1. JDK: Version 1.7 or above.
2. R Workbench or RStudio:This is the GUI used to run R scripts. download R


Installation and configuration of R

1.  Once you downloaded the R-3.4.*-win.exe then double click on this and install it.

2. After installation from desktop or installation_folder/bin folder (like on my system C:\Program Files\R\R-3.4.1\bin) double click on R.exe file. below is the R folder structure:

R Structure

After click on R.exe below will be the output.

R Terminal


Now installing Rserve package (Note: we also can use rJava package but in this example I am using Rserve)

1. On the R Terminal run the below command


during package installation it prompt you the list of mirrors you need to press “OK” button

Secure CRAN
Secure CRAN

Once package installation are done then now its time to load the package library using below command


Then type following command to start Rserve server at default port 6311.



Once R service started successfully below will be the output:

R Service Started
R Service Started

R file for integration sample.R and inside this file below is the function


That is all now you need to run an instance of Rserve server for R connection and run R Script from Java.

Note: If R service are not started then in Java you will get Connection reset error in java


Java setup for R Integration

1. To integrate with R we need to use R library. So you can use below dependency for R library



2. Now right the logic to create R Connection as below:

RConnection connection = new RConnection();

3. Once connection object is created we are good to execute .R files as below I am loading a .R file from specific location.


Note: if you got Error: ‘\U’ used without hex digits in character string starting “‘C:\U” error in R terminal then give the path like below using forward slash



4. In sample.R I have created a myAdd function to make the sum of two number and will return the sum of those values.

 int num1=10;
 int num2=20;
 int sum=connection.eval("myAdd("+num1+","+num2+")").asInteger();
 System.out.println("The sum is=" + sum);

5.  Complete java program is here

public class RScriptJava {
public static void main(String[] args) {
 RConnection connection;
 try {
 connection = new RConnection();
 int num1=10;
 int num2=20;
 int sum=connection.eval("myAdd("+num1+","+num2+")").asInteger();
 System.out.println("The sum is=" + sum);
 } catch (RserveException e) {
 } catch (REXPMismatchException e) {


6. below is the output of program

Rserve Output


I hope you also able to integrate R with java. Soon I am going to share complex integration between R and Java. Thank you

2,305 total views, 2 views today

Leave a Reply

Your email address will not be published. Required fields are marked *