Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

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
0
sirhallt
Asked:
sirhallt
2 Solutions
 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now