rJava (version 0.8-8)

J: High level API for accessing Java

Description

J creates a Java class reference or calls a Java method

Usage

J(class, 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 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)

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.

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.

See Also

.jcall, .jnew

Examples

Run this code
.jinit()

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

J("java.lang.Double")$parseDouble("10.2")
J("java.lang.Double", "parseDouble", "10.2" )

Double <- J("java.lang.Double")
Double$parseDouble( "10.2")

if (!nzchar(Sys.getenv("NOAWT"))) {
# 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)
}

Run the code above in your browser using DataCamp Workspace