Results 1 to 4 of 4
  1. #1
    vital101 is offline Member
    Join Date
    Oct 2007
    Posts
    2
    Rep Power
    0

    Default 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.

    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;
      }
    }
    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

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    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.

  3. #3
    vital101 is offline Member
    Join Date
    Oct 2007
    Posts
    2
    Rep Power
    0

    Default

    Do you think it'd be possible to do something like:
    Java Code:
    p = r.exec("R --no-save < test.r");
    //where test.r contains seq(1,10,1)
    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:

    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
    ....

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    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

  1. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  2. Inventory part 3 program problems
    By badness in forum New To Java
    Replies: 1
    Last Post: 12-17-2007, 07:00 AM
  3. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  4. Use a external file in my program
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:28 AM
  5. Execution cut
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-27-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •