Learn R Programming

J4R (version 1.0.8)

callJavaMethod: Call a Java method

Description

This function calls a public method in a particular class of object. If the javaObject parameters or the additional parameters (...) include vectors, the method is called several times and a vector of primitive or a list of java instances can be returned.

Usage

callJavaMethod(source, methodName, ..., affinity = 1)

Arguments

source

this should be either a java.list instance or a single java.object instance for non-static methods or a string representing the Java class name in case of static method

methodName

the name of the method

...

the parameters of the method

affinity

a parameter used by the mclapply.j4r function in case of multithreading.

Value

It depends on the method. It can return a primitive type (or a vector of primitive), a Java instance (or a list of Java instances) or nothing at all.

Details

There is no need to cast a particular parameter to a super class. Actually, the Java server tries to find the method that best matches the types of the parameters. Primitive type are converted on the fly, numeric to double, integer to int, logical to boolean and character to String. Factors are also converted to String.

When the source is a java.object instance, this function can be substituted for the $ operator.

See Also

J4R webpage

Examples

Run this code
# NOT RUN {
### starting Java
connectToJava(memorySize = 200)

### creating an empty ArrayList object
myList <- createJavaObject("java.util.ArrayList")

### adding 3 to the list
callJavaMethod(myList, "add", 3)

### adding 5 to the list
myList$add(3)

### shutting down Java
shutdownJava()

# }

Run the code above in your browser using DataLab