0

is there anyone, who is working with R and is using java code there? I mean calling Java code from R with "RJAVA" package.

I create my own package and there I have jar file of java code. (like there https://cran.r-project.org/web/packages/helloJavaWorld/vignettes/helloJavaWorld.pdf)

Then I have .r file and I want to call java method. Problem is when I want to return "Java Object of class". There in no problem with "I" as integer or "S" as string. I need Java Object of class in R to continue working with it . It is possible? I found I can return Java Object with return value "L" (e.g."Ljava/lang/Object") but it doesn't work.

This is my R code for calling java code:

FCA <- function(){

    a <- .jnew("fcamp/test/MainTest")  
    b <- .jcall(a, "S", "testFunction")
    c <- .jcall(a, "Lfcamp/input/Context;", "testFunction2")

    return(c) 
}

This is my error:

Error in .jcall(a, "Lfcamp/input/Context;", "testFunction2") : 
method testFunction2 with signature ()Lfcamp/input/Context; not found

Where is the mistake ? It is possible return Java Object of class to R and continue to work with it there?

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
L. Rohal
  • 3
  • 5
  • Your example should work with : c <- .jcall(a, "Lfcamp/input/Context;", "testFunction2") if the package path is correct. You need to prefix with "L" and suffix with ";" the full class name – tokiloutok Oct 07 '16 at 11:24
  • I have "L" and ";" there. And it still doesn't work. If you are sure that it is correct, it is possible that I have wrong java code. Thanks. – L. Rohal Oct 07 '16 at 13:33

1 Answers1

1

Here is an example (i hope complete) :

I have a java class :

package hello;

    public class Hello extends Object {

        public String sayHello2(String name) {
            String result = new String("Hello " + name);
            return result;
        }

        public Hello sayHello3(String name) {
            String result = new String("Hello " + name);
            return new Hello();
        }

    } 

sayHello3 returns an Hello object.

To create a jar :

   java -cp .   hello/Hello.java
    jar cvf Hello.jar hello/Hello.class

In my R session : A call with no java reference

library(rJava)
.jinit()
.jaddClassPath(dir( "path to jar", full.names=TRUE ))
.jclassPath()  # you should see your jar
hjw <- .jnew("Hello")     # create instance of hell/Hello class

outRef <- .jcall(hjw, "S", "sayHello2", "toto", evalString = FALSE)
.jstrVal(outRef)

[1] "Hello World"

And a call to a function returning a java reference:

outRef2 <- .jcall(hjw, "Lhello/Hello;", "sayHello3", "Universe", evalString = T)
.jstrVal(outRef2)
outRef3 <- .jcall(outRef2, "S", "sayHello2", "New Universe", evalString = FALSE)
.jstrVal(outRef3)

returning :

"hello.Hello@74a14482"
"Hello New Universe"
tokiloutok
  • 467
  • 5
  • 14
  • 1
    Thank you...it helps me a lot! :) I have one more question. If I need to return from java something like "List " what am i supposed to do? Is there some return type for Lists? – L. Rohal Nov 08 '16 at 08:20