Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-30-2007, 05:02 PM
Member
 
Join Date: Oct 2007
Posts: 2
vital101 will become famous soon enough
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.

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

Quote:
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-30-2007, 05:16 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-30-2007, 05:27 PM
Member
 
Join Date: Oct 2007
Posts: 2
vital101 will become famous soon enough
Do you think it'd be possible to do something like:
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:

Quote:
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
....
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-30-2007, 06:17 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
Inventory part 3 program problems badness New To Java 1 12-17-2007 08:00 AM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM
Use a external file in my program romina New To Java 1 08-07-2007 06:28 AM
Execution cut Eric Advanced Java 1 06-27-2007 04:52 PM


All times are GMT +3. The time now is 01:49 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org