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.

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 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

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 Financial Econometrics & Derivatives 
# ML/DL using R, Python, Tensorflow by Sang-Heon Lee 
#
# https://shleeai.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\)


4 comments:

  1. For more convenient syntax, take a look at the "J" function. Example:

    > x <- new(J("java.lang.Double"),.5)
    > x
    [1] "Java-Object{0.5}"
    > x$doubleValue()
    [1] 0.5

    ReplyDelete
    Replies
    1. Thank you for helpful comments. I think it is very useful and convenient approach. I'll investigate the usage of "J" function.

      Delete
  2. Hi,

    How can I change the directory to save HTLM in Onedrive?


    Many thanks

    G.C.

    ReplyDelete
    Replies
    1. Thank you for visiting my blog.
      I can't understand what HTML means.

      If you intend to load jar file from One drive, I think that the full path with jar file name would be used. But I am not sure because I also have no experience in doing that.

      I'm sorry I couldn't be of more help to you.

      Delete