J creates a Java class reference or calls a Java method

J(class, method, ..., class.loader=.rJava.class.loader)

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 J results in a method call, otherwise it just creates a class name reference.

...

optional parameters that will be passed to the method (if the method argument is present)

class.loader

optional, custom loader to use if a class look-up is necessary (i.e., if class is 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.

See also

Examples


if (!nzchar(Sys.getenv("NOAWT"))) {
  f <- new(J("java.awt.Frame"), "Hello")
  f$setVisible(TRUE)
}

J("java.lang.Double")$parseDouble("10.2")
#> [1] 10.2
J("java.lang.Double", "parseDouble", "10.2" )
#> [1] 10.2

Double <- J("java.lang.Double")
Double$parseDouble( "10.2")
#> [1] 10.2

# 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)