Hey everyone,
I'm working on a project right now where I need to be able to access an external program from inside my Java app. My problem is that as soon as i launch the program, any other commands I issue are treated and strictly command line actions and are not executed inside the program. Here's what i've got.
import java.io.*;
public class ExecDemoWait {
public static void main(String argv[]) throws IOException {
Runtime r = Runtime.getRuntime();
Process p; // Process tracks one external native process
BufferedReader is; // reader for output of process
String line;
/******************************
* PROBLEM IS IN THE NEXT TWO LINES
******************************/
p = r.exec("R --no-save");
p = r.exec("seq(1,3,1)");
System.out.println("In Main after exec");
is = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = is.readLine()) != null)
System.out.println(line);
System.out.println("In Main after EOF");
System.out.flush();
try {
p.waitFor(); // wait for process to complete
} catch (InterruptedException e) {
System.err.println(e); // "Can'tHappen"
return;
}
System.err.println("Process done, exit status was " + p.exitValue());
return;
}
}
As you can see, I first launch R, then I try and execute a statement that produces a sequence of numbers. However, when I run the program I get...
Exception in thread "main" java.io.IOException: Cannot run program "seq(1,3,1)": java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java :459)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at ExecDemoWait.main(ExecDemoWait.java:23)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
at java.lang.ProcessImpl.start(ProcessImpl.java:65)
at java.lang.ProcessBuilder.start(ProcessBuilder.java :452)
... 4 more
From the first line, we can see that it's trying to run the "sequence.." argument as a stand-alone command line argument. How can I get the "sequence.." command to execute inside of R?
Thanks for the help.
/vital101