Using Java Functions in JAR File from R Using rJava

This post explains how to call Java functions from jar file in R. This is useful especially when derivatives pricing or risk calculation engine already have been developed well in the form of Java in your company.

Calling Java functions in JAR file from R

Since we have made a sample jar file (aObba.jar) in the following previous post, we reuse sample functions from that jar file.

aObba.jar file contains three functions (func1, func2, func3) for scalar or vector operations.

package aObba;
public class CObba {
    public static double func1(double a) {return (a*a);}
    public static double func2(double a, double b) {return (a*b);}
    public static double[] func3(double[] a, double[] b) {
        double[] c = new double[a.length];
        for(int i=0;i<a.length;i++) {c[i] = a[i]*b[i];}
        return (c);

We try to use these java functions in this jar file in R like the following figure.

R code

We can easily carry out this job using rJava R package. The next R code is simple and straightforward but there are three things to note.

  1. .jaddClassPath() is called with jar file name with its full path explicitly
  2. .jnew() is called with packagename.classname
  3. .jcall() is called with “[D” or as.double() explicitly when array input or output is used
# Quantitative ALM, Financial Econometrics & Derivatives 
# ML/DL using R, Python, Tensorflow by Sang-Heon Lee 
# Call Java function in jar file from R
#========================================================#  # clear all graphs
rm(list = ls()) # remove all files from your workspace
.jinit()        # JVM start
.jclassPath()   # get default class path
# 1) Add directory in which *.jar file is located
.jclassPath()   # get class path with added path
# 2) Instantiate a class object which we will use
# rule : packagename.classname should be used
Obba1 <- .jnew("aObba.CObba")
# 3) Call Java functions in the class object
#    "D" : double
#   "[D" : double []
# "[D[D" : double [][]
.jcall(Obba1, "D", "func1", 5)
.jcall(Obba1, "D", "func1", 5.5)
.jcall(Obba1, "D", "func2", 4, 5)
.jcall(Obba1, "D", "func2", 4.4, 5.5)
# In case of double array input, 
# "as.double" should be used
.jcall(Obba1, "[D","func3", 
       as.double(1:5+0.01), as.double(11:15+0.02))

As can be seen, we can get the correct outputs which are returned from calling Java functions in jar file.

> .jcall(Obba1, "D", "func1", 5)
[1] 25
> .jcall(Obba1, "D", "func1", 5.5)
[1] 30.25
> .jcall(Obba1, "D", "func2", 4, 5)
[1] 20
> .jcall(Obba1, "D", "func2", 4.4, 5.5)
[1] 24.2
> .jcall(Obba1, "[D","func3", 
+        as.double(1:5+0.01), as.double(11:15+0.02))
[1] 11.1302 24.1602 39.1902 56.2202 75.2502

Concluding Remarks

From this post, we have learned how to call Java functions in jar file from R easily with the help of rJava R package. This method is quite useful when complicated calculation modules are already implemented by using Java.

For additional insight on this topic, visit

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.