?
Solved

passing user input to command line programs using exec()

Posted on 2003-03-17
6
Medium Priority
?
412 Views
Last Modified: 2010-03-31
I have a script that runs on the command line.  I would like to call this script using the Runtime.exec() method however, my problem is that the script has points where it asks for user input.  I would like to know if there is a way that Java can provide the user input and not have them come in as arguments to the script.  I need to be able to view the results of the script and make a user choice based on the output.  Currently when it hits the points where the script asks for user input it stops and I cannot get it to go any further.  Thanks
0
Comment
Question by:sirhallt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Accepted Solution

by:
vanatteveldt earned 152 total points
ID: 8155582
Command line applications actually work via the input and output streams. Suppose the session would look like:

Application:"Bla bla bla welcome to this app
Do you want to continue?"
User: "Yes"
Application:"Ok here we go where are you from?"
User:"Alabama"

Then what is really happening is that the application sends these two lines to your input stream, and waits for something from your output stream, in this case "Yes\n".

Thus, to 'mimick' a user you will have to send things over the output stream to the process. If you know what to send to the program beforehand you can simply immeditately send it, i.e. suppose your process is called "p":

BufferedWriter out
   = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
String msg = "Yes\n";
out.write(msg, 0, msg.length());
out.flush;

If you need to react to the program, you'd need to also read your inputstream using a similar construction:

BufferedReader in
 = new BufferedReader(new InputStreamReader(p.getInputStream()));

And then you can use in.readLine() to read lines of output from the program.

"Hope this helps"

Wouter
0
 
LVL 1

Assisted Solution

by:wolfc
wolfc earned 148 total points
ID: 8157706
Working example: (on Linux)

import java.io.*;
public class X
{
        public static void main(String args[])
                throws IOException
        {
                Process p = Runtime.getRuntime().exec("cat");
                PrintStream ps = new PrintStream(p.getOutputStream());
                ps.println("Hello world");
                ps.flush();
                ps.close();
                System.out.println("OUTPUT:");
                InputStream in = p.getInputStream();
                int ch;
                while((ch = in.read()) >= 0)
                {
                        System.out.write(ch);
                }
                System.out.flush();
        }
}
0
 

Expert Comment

by:CleanupPing
ID: 9058634
sirhallt:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 

Expert Comment

by:vanatteveldt
ID: 9058669
I think my answer is a good (slightly theoretical) answer to the question, and wolfc provided a good working example - split points?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9726961
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split between vanatteveldt and wolfc

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question