From simon.urbanek at r-project.org  Wed Jan 16 16:16:26 2013
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Wed, 16 Jan 2013 10:16:26 -0500
Subject: [Rd] How to call Java main method using rJava package?
In-Reply-To: <CACq8Ys2TSKahaeVTU6m-zvzh+kXvC1OFgrw0oNQWP16etkqi_g@mail.gmail.com>
References: <CACq8Ys2TSKahaeVTU6m-zvzh+kXvC1OFgrw0oNQWP16etkqi_g@mail.gmail.com>
Message-ID: <2A6B69EE-64BD-4BBA-BBFD-A9070CEF9663@r-project.org>


On Jan 16, 2013, at 7:32 AM, Gaurav Dasgupta wrote:

> Hi,
> 
> I am trying to integrate my Java program with R using rJava package. I want
> to execute the whole Java program from R. The main() method in my Java code
> calls all the other defined methods. So, I guess I have to call the main()
> method in .jcall.
> 
> An example Java code:
> 
> *class A {
>        public static int mySum(int x, int y) {
>                return x+y;
>        }
>        public static void main (String[] arg) {
>                System.out.println("The sum is " + mySum(2, 4));
>        }
> }*
> 
> I can do the following to call the mySum() method:
> *.jcall(obj, "I", "mySum", as.integer(2), as.integer(4))*
> This will give the output *6*.
> 
> But can some one explain me that how exactly I can execute this program to
> print *The sum is 6* from R? OR how can I call the main method?

.jcall("A","V","main",.jarray(list(), "java/lang/String")

But note that System.out is sent to stdout by Java so it won't output in the R console as R output so you should really avoid it.

You may consider using the stats-rosuda-devel mailing list for rJava questions.

Cheers,
Simon


> I am a beginner in R.
> 
> Thanks,
> Gaurav
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



[Rd] How to call Java main method using rJava package?
Simon Urbanek