High level API for accessing Java
J.RdJ creates a Java class reference or calls a Java method
Arguments
- class
java object reference or fully qualified class name in JNI notation (e.g "java/lang/String" ) or standard java notation (e.g "java.lang.String")
- method
if present then
Jresults in a method call, otherwise it just creates a class name reference.- ...
optional parameters that will be passed to the method (if the
methodargument is present)- class.loader
optional, custom loader to use if a class look-up is necessary (i.e., if
classis a string)
Details
J is the high-level access to Java.
If the method argument is missing then code must be a
class name and J creates a class name reference that can be
used either in a call to new to create a new Java object
(e.g. new(J("java.lang.String"), "foo")) or with $
operator to call a static method
(e.g. J("java.lang.Double")$parseDouble("10.2").)
If the method argument is present then it must be a string
vector of length one which defines the method to be called on the
object.
Value
If method is missing the the returned value is an object of
the class jclassName. Otherwise the value is the result of
the method invocation. In the latter case Java exceptions may be
thrown and the function doesn't return.
Note
J is a high-level API which is slower than .jnew
or .jcall since it has to use reflection to find the
most suitable method.
Examples
if (!nzchar(Sys.getenv("NOAWT"))) {
f <- new(J("java.awt.Frame"), "Hello")
f$setVisible(TRUE)
}
#> Error in .jcall("RJavaTools", "Ljava/lang/Object;", "newInstance", class, .jarray(p, "java/lang/Object", dispatch = FALSE), .jarray(pc, "java/lang/Class", dispatch = FALSE), evalString = FALSE, evalArray = FALSE, use.true.class = TRUE): RcallMethod: cannot determine object class
J("java.lang.Double")$parseDouble("10.2")
#> Error in .jfindClass(as.character(class), class.loader = class.loader): java.lang.NoClassDefFoundError: RJavaToolsjclassName(class, class.loader = class.loader)new("jobjRef", jobj = <pointer: 0x590bf6c97f52>, jclass = "java/lang/NoClassDefFoundError")
J("java.lang.Double", "parseDouble", "10.2" )
#> Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, .jcast(if (inherits(o, "jobjRef") || inherits(o, "jarrayRef")) o else cl, "java/lang/Object"), .jnew("java/lang/String", method), j_p, j_pc, use.true.class = TRUE, evalString = simplify, evalArray = FALSE): RcallMethod: cannot determine object class
Double <- J("java.lang.Double")
#> Error in .jfindClass(as.character(class), class.loader = class.loader): java.lang.NoClassDefFoundError: RJavaToolsjclassName(class, class.loader = class.loader)new("jobjRef", jobj = <pointer: 0x590bf6c97f4a>, jclass = "java/lang/NoClassDefFoundError")
Double$parseDouble( "10.2")
#> Error: object 'Double' not found
# String[] strings = new String[]{ "string", "array" } ;
strings <- .jarray( c("string", "array") )
# this uses the JList( Object[] ) constructor
# even though the "strings" parameter is a String[]
l <- new( J("javax.swing.JList"), strings)
#> Error in .jcall("RJavaTools", "Ljava/lang/Object;", "newInstance", class, .jarray(p, "java/lang/Object", dispatch = FALSE), .jarray(pc, "java/lang/Class", dispatch = FALSE), evalString = FALSE, evalArray = FALSE, use.true.class = TRUE): RcallMethod: cannot determine object class