Introduction
This primer shall consist of two parts and its goal is to provide a walk-through of using resources developed in Java from R. It is structured as more of a “note-to-future-self” rather than a proper educational article, I however hope that some readers may still find it useful. It will also list a set of references that I found very helpful, for which I thank the respective authors.
The primer is split into 2 posts:
In this first one we talk about using of the rJava package to create objects, call methods and work with arrays, we examine the various ways to call Java methods and calling Java code from R directly via execution of shell commands.
In the second one we discuss creating and using custom .jar archives within our R scripts and packages, handling of Java exceptions from R and a quick look at performance comparison between the low and high-level interfaces provided by rJava.
R 0) {
stringArg = args[0];
}
System.out.println(&quot;Hello, World. &quot; + stringArg);
}
}
Compilation and execution via bash commands
Now that we have our dummy class ready, we can put together the commands and test them by just executing via a shell, or for RStudio fans, we can test the commands via RStudio’s cool Terminal feature. First, the compilation command, which may look something like the following, assuming that we are in the correct working directory:
$ javac DummyJavaClassJustForFun/HelloWorldDummy.java
Now that we have the class compiled, we can execute the main method, with and without the argument provided:
$ java DummyJavaClassJustForFun/HelloWorldDummy
$ java DummyJavaClassJustForFun/HelloWorldDummy &quot;I like winter&quot;
In case we need to compile and run with more .jars that are in folder jars/, we specify the folder using -cp (class path):
$ javac -cp &quot;jars/*&quot; DummyJavaClassJustForFun/HelloWorldDummy.java
$ java -cp &quot;jars/*:compile/src&quot; DummyJavaClassJustForFun/HelloWorldDummy
Compilation and execution of Java code from R
Now that we have tested our commands, we can use R to do the compilation via the system function. Do not forget to cd into the correct directory within a single system call if needed:
system('cd data/; javac DummyJavaClassJustForFun/HelloWorldDummy.java')
After that we can also execute the main method, and the main method with one argument specified, just like we did it outside of R, once again using cd to enter the proper working directory if needed:
system('cd data/; java DummyJavaClassJustForFun/HelloWorldDummy')
system('cd data/; java DummyJavaClassJustForFun/HelloWorldDummy &quot;Also I like winter&quot;')
The rJava package - an R to Java interface
The rJava package provides a low-level interface to Java virtual machine. It allows creation of objects, calling methods and accessing fields of the objects. It also provides functionality to include our java resources into R packages easily.
We can install it with the classic:
install.packages(&quot;rJava&quot;)
Note the system requirement Java JDK 1.2 or higher and for JRI/REngine JDK 1.4 or higher. After attaching the package, we also need to initialize a Java Virtual Machine (JVM):
## Attach rJava and Init a JVM
library(rJava)
.jinit()
In case of issues with attaching the package using library, one can refer to this helpful StackOverflow thread.
Creating Java objects with rJava
We will now very quickly go through the basic uses of the package. The .jnew function is used to create a new Java object. Note that the class argument requires a fully qualified class name in Java Native Interface notation.
# Creating a new object of java.lang class String
sHello