Using Java functions from JAR file in R using rJava

[This article was first published on K & L Fintech Modeling, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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. It is more efficient to use time-tested existing module than to develop new module.


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



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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);
    }
}
 
cs


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

Calling Java functions from JAR file in R using rJava


R code


We can easily carry out this job owning to rJava R package. The next R code is simple and straightforward but there are two 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#========================================================#
# Quantitative ALM, Financial Econometrics & Derivatives 
# ML/DL using R, Python, Tensorflow by Sang-Heon Lee 
#
# https://kiandlee.blogspot.com
#——————————————————–#
# Call Java function in jar file from R
#========================================================#
 
graphics.off()  # clear all graphs
rm(list = ls()) # remove all files from your workspace
 
library(rJava)
 
 
.jinit()        # JVM start
.jclassPath()   # get default class path
 
#————————————————–
# 1) Add directory in which *.jar file is located
#————————————————–
 
.jaddClassPath(“D:/SHLEE/sh_jar/aObba.jar”)
.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”45)
.jcall(Obba1, “D”“func2”4.45.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))
 
cs


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

1
2
3
4
5
6
7
8
9
10
11
12
> .jcall(Obba1, “D”“func1”5)
[125
> .jcall(Obba1, “D”“func1”5.5)
[130.25
> .jcall(Obba1, “D”“func2”45)
[120
> .jcall(Obba1, “D”“func2”4.45.5)
[124.2
> .jcall(Obba1, “[D”,“func3”
+        as.double(1:5+0.01), as.double(11:15+0.02))
[111.1302 24.1602 39.1902 56.2202 75.2502
> 
cs


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. \(\blacksquare\)


To leave a comment for the author, please follow the link and comment on their blog: K & L Fintech Modeling.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)