RServe Java example program

September 13, 2016

(This article was first published on R – StudyTrails, and kindly contributed to R-bloggers)


The steps to install and start RServe are shown in the previous tutorial. This tutorial explains how to create a java program that can talk to R using RServe on a linux platform. The steps to set up are.

  1. Lets assume for this tutorial that the working directory in linux where you want to setup the program is HOME/RServe. Create a directory inside it called lib. The path of lib then is HOME/RServe/lib
  2. When you install RServe the process downloads the RServe tar and puts its in a directory that you specify. If you dont know where the RServe tar is download it again from here. untar the file using ‘tar -xvf RServe_x.tar.gz‘. Copy REngine.jar and RServe.jar from Rserve_x/clients/java-new to HOME/RServe/lib. Your working directory is now ready to run RServe java programs.

Example Program

Here’s a sample program, that gets the R version using RServe. Its a very basic program but the idea is to get RServe and Java integration up and running for more serious stuff that we will look at later

import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

public class RServeExample1 {

	public static void main(String[] args) throws RserveException,
			REXPMismatchException {
		RConnection c = new RConnection();
		REXP x = c.eval("R.version.string");

Create a file named and copy the code above. Put the file in HOME/RServe. Compile the code using

javac -cp .:lib/*

Start RServe as described in the

Run the class using

java -cp .:lib/* RServeExample1

The post RServe Java example program appeared first on StudyTrails.

To leave a comment for the author, please follow the link and comment on their blog: R – StudyTrails. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)