<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/a-primer-in-using-java-from-r-part-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://jozefhajnala.gitlab.io/r/r902-primer-java-from-r-2/"> Jozef's Rblog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h1>Introduction</h1>
<p>In this part of the primer 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.</p>
<p>In the <a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r901-primer-java-from-r-1/" target="_blank">first part</a> we talked about using the rJava package to create objects, call methods and work with arrays, we examined the various ways to call Java methods and calling Java code from R directly via execution of shell commands.</p>
<div>
<img alt="R &lt;3 Java, or maybe not?" src="https://i2.wp.com/jozefhajnala.gitlab.io/r/img/r901-01-r-java.gif?w=456&ssl=1" data-recalc-dims="1">
<p>R &lt;3Java, or maybe not?</p>
</div>
</div>
<div>
<h1>Contents</h1>
<ol>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r902-primer-java-from-r-2/#using-rjava-with-custom-built-classes" target="_blank">Using rJava with custom built classes</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r902-primer-java-from-r-2/#very-quick-look-at-performace" target="_blank">Very quick look at performace</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r902-primer-java-from-r-2/#usage-of-jars-in-r-packages" target="_blank">Usage of jars in R packages</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r902-primer-java-from-r-2/#handling-java-exceptions-in-r" target="_blank">Handling Java exceptions in R</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r902-primer-java-from-r-2/#references" target="_blank">References</a></li>
</ol>
</div>
<div>
<h1>Using rJava with custom built classes</h1>
<div>
<h2>Preparing a .jar archive for use</h2>
<p>Getting back to our example with running the <code>main</code> method of our <code>HelloWorldDummy</code> class from the <a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r901-primer-java-from-r-1/" target="_blank">first part of this primer</a>, in practice we most likely want to actually create objects and invoke methods for such classes rather than simply call the main method.</p>
<p>For our resources to be available to rJava, we need to create a .jar archive and add it to the class path. An example of the process can be as follows. Compile our code to create the class file, and jar it:</p>
<pre><code>$ javac DummyJavaClassJustForFun/HelloWorldDummy.java
$ cd DummyJavaClassJustForFun/
$ jar cvf HelloWorldDummy.jar HelloWorldDummy.class</code></pre>
</div>
<div>
<h2>Adding the .jar file to the class path</h2>
<p>Within R, attach rJava, initialize the JVM and investigate our current class path using <code>.jclassPath</code>:</p>
<pre><code>library(rJava)
.jinit()</code></pre>
<pre><code>.jclassPath()</code></pre>
<p>Now, we add our newly created .jar to the class path using <code>.jaddClassPath</code>:</p>
<pre><code>.jaddClassPath(paste0(jardir, &quot;HelloWorldDummy.jar&quot;))</code></pre>
<p>If this worked, we can see the added jar(s) in the class path if we call <code>.jclassPath()</code> again.</p>
</div>
<div>
<h2>Creating objects, investigating methods and fields</h2>
<p>Now that we have our .jar in the class path, we can create a new Java object from our class:</p>
<pre><code>dummyObj &lt;- .jnew(&quot;DummyJavaClassJustForFun/HelloWorldDummy&quot;)
str(dummyObj)</code></pre>
<pre><code>## Formal class 'jobjRef' [package &quot;rJava&quot;] with 2 slots
##   ..@ jobj  :</code></pre></div></div>