0

I have an ant command for build process when I am executing that command from command prompt in between of execution it is prompting me for some password and when I enter the password and press enter key it continues the execution and giving me the successful message.

Now I want to do the same thing in java programatically using Runtime.execute(...); and it is working fine till prompting the password(I am reading the output of command prompt) but I don't know how to show a dialog box when cmd output is prompting for password where I can enter my password and with that that password my program continues the execution and complete successfully. I want to do same thing what I am doing manually from command prompt.

So question is Is it possible to enter parameters in between of command execution by Runtime.execute and how to resume the command execution passing theat paramenter if if yes how ?

It is simple a swing application which is executing the command and I want to use a Swing Ui dialog for taking password.

Updated my code as suggested by Andy Thomas :

Process p1 =Runtime.getRuntime().exec("cmd /C my command", null, myProject);
BufferedReader in = new BufferedReader(  
                            new InputStreamReader(p1.getInputStream()));  
String line = null;  

while ((line = in.readLine()) != null) {  
   System.out.println(line);
   if(line.contains("Please enter password")){
       System.out.println("Prompting for password ");
       OutputStream child_stdin = p1.getOutputStream();
       BufferedWriter child_writer = new BufferedWriter(new OutputStreamWriter( child_stdin ));
       child_writer.write( "password123" );
        child_writer.flush();

            }

        }  

    }

But did not work.

Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • do you want to user enter in console? or in a swing gui? – nachokk Jul 15 '13 at 19:56
  • in swing gui, i want to cate a dialog box using swing only and user will enter pass and will click ok and pressing ok will continue the execution with given value of password. – Piyush Agarwal Jul 15 '13 at 19:58
  • This is 100% possible, all you need is a simple action listener for the login button and then you'll need to validate the password. If all else checks out the just run your executable. – Tdorno Jul 15 '13 at 19:59
  • it's simple, just create for example a JDialog add 2 textfield one for user anohter for pass , a button, and `addActionListener` to the button – nachokk Jul 15 '13 at 20:01
  • how can i pass the parameters in between execution of cmd commad ? – Piyush Agarwal Jul 15 '13 at 20:01
  • im not quite sure understanding your problem, you want to read input from cmd ? – nachokk Jul 15 '13 at 20:03
  • Are you trying to run the command, then show a dialog for a command-line prompt issued by the command? – Andy Thomas Jul 15 '13 at 20:06
  • I have updated the question let me know are you getting what i want – Piyush Agarwal Jul 15 '13 at 20:12
  • Have you seen this? [http://stackoverflow.com/questions/4112470/java-how-to-both-read-and-write-to-from-process-thru-pipe-stdin-stdout](http://stackoverflow.com/questions/4112470/java-how-to-both-read-and-write-to-from-process-thru-pipe-stdin-stdout) – c.s. Jul 15 '13 at 20:44

2 Answers2

1

When you call Runtime.exec(), you get back a Process object representing the child process.

  1. Read from the child process's output stream -- which is provided by Process.getInputStream() -- until you detect the request for a password.
  2. Ask the user for a password, preferably using JPasswordField so that the password will be hidden.
  3. Feed the password ot the process's input stream, provided by Process.getOutputStream().
  4. Keep reading from the child process's output stream, or the child process may block.

EDIT: Here's a sketch (not tested) of how you would you write a String userPassword to the child process, as requested by your comment below. The ... elides the part that reads from the child process's output stream, without which the child will likely block.

    Process process = Runtime.getRuntime().exec("...");
    ...
    OutputStream child_stdin = process.getOutputStream();
    BufferedWriter child_writer = new BufferedWriter(new OutputStreamWriter( child_stdin ));
    child_writer.write( userPassword );
    child_writer.newLine();  // suggested by OP
    child_writer.flush();
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Edited significantly after clarification by OP. – Andy Thomas Jul 15 '13 at 20:18
  • can i have some code snippet for how to feed password using Process.getOutputStream i did not get anything. – Piyush Agarwal Jul 15 '13 at 20:26
  • nothing happens what should i do to continue the command further after adding the above code snippet. The out is stucked there only it is not going further. – Piyush Agarwal Jul 15 '13 at 20:48
  • Have you replaced the ... above with something to read the child process's output stream? – Andy Thomas Jul 15 '13 at 20:55
  • i have updated the question with code you have suggested. Please check whether i am write or wrong. – Piyush Agarwal Jul 15 '13 at 21:08
  • have you checked my updated answer with code i think i am right as you said but not wroked – Piyush Agarwal Jul 15 '13 at 21:18
  • Okay! Looks good at a glance. I would move the declaration of child_stdin outside the loop, alongside "in". What behavior are you seeing - are you successfully reading anything? If you can't read anything from getInputStream(), try reading from getErrorStream(), which provides the stderr from the process. (If you use ProcessBuilder to make your process, you can tie stderr and stdout into one stream.) – Andy Thomas Jul 15 '13 at 21:28
  • I am getting output till this after that i have written the code present in my question but i think we missing the continue execution after entering the password as we are pressing enter key in case of cmd. -post-package: -release-prompt-for-password: [input] Please enter password : – Piyush Agarwal Jul 15 '13 at 21:29
  • Have you tried writing a newline at the end of the password: child_writer.write( "password123\n" ). – Andy Thomas Jul 15 '13 at 21:33
  • I tried writing this after writing password nothing happnens :( `child_writer.newLine();` – Piyush Agarwal Jul 15 '13 at 21:45
  • Did you write the newline and then flush(), or vice-versa? – Andy Thomas Jul 15 '13 at 21:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33571/discussion-between-pyus13-and-andy-thomas) – Piyush Agarwal Jul 16 '13 at 16:12
  • 1
    finally it is working fine @Andy you just add `child_writer.newLine();` before flush so i can accept your answer. – Piyush Agarwal Jul 16 '13 at 17:37
  • @pyus13 - your edit was rejected by other stackoverflow members. I added it back by hand, with attribution. Thanks for making the answer better for people looking at it in the future. – Andy Thomas Jul 23 '13 at 13:14
1

As an alternative, load the password into ant from a secure file, as shown here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045