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.
We try to use these java functions in this jar file in R like the following figure.
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.
- .jaddClassPath() is called with jar file name with its full path explicitly
- .jnew() is called with packagename.classname
- .jcall() is called with “[D” or as.double() explicitly when array input or output is used
As can be seen, we can get the correct outputs which are returned from calling Java functions in jar file.
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\)