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 toCLASSPATH
rJava::.jaddClassPath(pathToJar)
- check inside
JAR
file what is the main class. Look intoMETA-INF/MANIFEST.MF
file to identify the main class. (In this casecom.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
Automatically Detect Date Columns When Reading a File into a Data.Frame
Plot Table Objects with Ggplot
R Looping Through in Survey Package
How Does Settimelimit Work in R
How to Calculate the Median on Grouped Dataset
How to Change Factor Labels into String in a Data Frame
How to Run a Job Array in R Using the Rscript Command from the Command Line
Alpha Aesthetic Shows Arrow's Skeleton Instead of Plain Shape - How to Prevent It
Sort Boxplot by Mean (And Not Median) in R
R Dplyr Filter Based on Matching Search Term with First Words of Any Work in Select Columns
Flatten Nested Lists in a List
Why Is Date Is Being Returned as Type 'Double'
Space Between Gpplot2 Horizontal Legend Elements
R: Is There a Good Replacement for Plyr::Rbind.Fill in Dplyr