Results 1 to 4 of 4
- 10-30-2007, 04:02 PM #1
Member
- Join Date
- Oct 2007
- Posts
- 2
- Rep Power
- 0
External Program execution problems
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.
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...Java Code: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; } }
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?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
Thanks for the help.
/vital101
- 10-30-2007, 04:16 PM #2
I guess you will need to get input stream of the process and write your commands through it. Search internet for examples to use input streams of the processes.
- 10-30-2007, 04:27 PM #3
Member
- Join Date
- Oct 2007
- Posts
- 2
- Rep Power
- 0
Do you think it'd be possible to do something like:
When I do this on the command line directly, it works fine. However, when I do this from within Java, it ignores (and tells me it's ignoring) everything from the "<" forward. Here's the error:Java Code:p = r.exec("R --no-save < test.r"); //where test.r contains seq(1,10,1)
In Main after exec
ARGUMENT '<' __ignored__
ARGUMENT 'test.r' __ignored__
R version 2.5.1 (2007-06-27)
Copyright (C) 2007 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
....
- 10-30-2007, 05:17 PM #4
Yes, i am sure it is possible but it is not working as in the first way we think.
Check the following way for several examples. As far as i see (after a quick look), the page also has examples for processes with parameters...
Execute an external program - Real's Java How-to
Similar Threads
-
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
Inventory part 3 program problems
By badness in forum New To JavaReplies: 1Last Post: 12-17-2007, 07:00 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM -
Use a external file in my program
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:28 AM -
Execution cut
By Eric in forum Advanced JavaReplies: 1Last Post: 06-27-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks