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 Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight 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 SH Fintech Modeling and is being posted with permission from SH Fintech Modeling. The views expressed in this material are solely those of the author and/or SH Fintech Modeling and IBKR 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 sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. 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.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.