In this final part of the rJava series, learn about checking for exceptions, using arrays, and other important tasks.
In » Getting Started with R Using Java , » you learned how to initialize the JVM to use Java with R, set the classpath, and invoke a Java object you created. In » Using Java Strings and the Swing API in R , » you learned to use Strings, create a Java Swing application, and find if an object is an instance of a class. In this tutorial, we shall discuss using the rJava R package for exception handling, arrays, collections, casting and comparing Java objects and also to get System properties. This tutorial has the following sections.
As mentioned before, the .jnew(class, …, check=TRUE, silent=!check) function has the provision to check for exceptions by setting the check arg to TRUE , which is the default. Because the « silent » arg is !check by default, an error message gets output if an error exists. As an example, create a new Java object from the non-existent class hello/world with check=TRUE.
The following output indicates an error.
The following functions are provided to check for exceptions and throw exceptions.
If check=FALSE in the .jnew function call, an error message is not generated, the exception is not cleared, and the exception object may be obtained. As an example, call.jnew with a non-existent class and check=FALSE. No error message is generated.
To find if a Java exception was raised, call the .jgetEx function on the implicit exception object e and, if the exception object is not null, output a message to indicate that an exception was raised.
The exception message may be output.
The .jcheck function clears the exception and, if called twice consecutively, an output of FALSE or 0 indicates the same.
The rJava package provides the .jarray function to create a Java array reference from a vector or a list of Java references.