passing user input to command line programs using exec()

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
sirhalltAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vanatteveldtCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wolfcCommented:
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
CleanupPingCommented:
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
vanatteveldtCommented:
I think my answer is a good (slightly theoretical) answer to the question, and wolfc provided a good working example - split points?
0
jimmackCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.