Frustration Using Rjava to Call a Third Party Java Jar

Frustration using rJava to call a third party Java jar

Let me preface my answer by saying that I'm no expert in Java / rJava, so apologies if this isn't 100% correct. I hope it's a step in the right direction though.

Start by unzipping gsrad_sample.jar to C:/gsrad (or adjust your paths based on where you unzip it). Then add all the contents of C:/gsrad/lib to your class path:

library(rJava)
.jinit()
.jaddClassPath(dir( "C:/gsrad/lib", full.names=TRUE ))
.jclassPath()
.jnew( "cra/clima/gsrad/GSRBristowCampbellStrategy" )

Replicating a java -jar execution through rJava

Executing JAR file is (essentially) running class file that is embedded inside JAR.

Instead of calling system and executing it as external application, you can do following:

  • make sure to add your JAR file to CLASSPATH
rJava::.jaddClassPath(pathToJar)
  • check inside JAR file what is the main class. Look into META-INF/MANIFEST.MF file to identify the main class. (In this case com.gitlab.pdftk_java.pdftk)
  • instantiate class inside R.
newObj = rJava::.jnew('com/gitlab/pdftk_java/pdftk')
  • run the class following way: http://www.owsiak.org/running-java-code-in-r/

Update

Running JAR file (calling main method of Main-class) is the same things as calling any other method inside Java based class. Please note that main method takes array of Strings as argument. Take a look here for sample: http://www.owsiak.org/running-jar-file-from-r-using-rjava-without-spawning-new-process/

newObj$main(rJava::.jarray('--version'))

For this specific case if you look at the source code for this class, you'll see that it terminates the session

  public static void main(String[] args) {
System.exit(main_noexit(args));
}

This will also terminate your R session. Since all main function does it to call main_noexit then exit, you can replace main with main_noexit in the code above.

newObj$main_noexit(rJava::.jarray('--version'))

How to call methods with varargs in RJava with zero values?

As pointed out in https://www.rforge.net/doc/packages/rJava/jarray.html, you should supply a list in the call to .jarray, possibly with a single NULL element

How to set the classpath for rJava in R?

Try .jaddClassPath

.jaddClassPath("blah.jar")

p-value calculation using Java

I think your are facing problem with retrieving and printing values from file. Below program gives output in required format :

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Abc {

public static void main(String[] args) {

BufferedReader br = null;
String sCurrentLine;

try {

br = new BufferedReader(new FileReader("C:/Documents and Settings/Admin/Desktop/test.txt"));

// Override Default Header Row
br.readLine();
System.out.println("Name" + "\t" + "pValue");

while ((sCurrentLine = br.readLine()) != null) {
int i = 0;
String str[] = sCurrentLine.split("\t");

System.out.print(str[i] + "\t");

Double dValue1 = Double.parseDouble(str[++i]);
Double dValue2 = Double.parseDouble(str[++i]);
Double dValue3 = Double.parseDouble(str[++i]);

// Do pValue Calc here
Double pValue = 1.2334;

System.out.println(pValue);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}


Related Topics



Leave a reply



Submit