Enter parameter and continue the execution of cmd command through java swing application in Runtime.execute() -


i have ant command build process when executing command command prompt in between of execution prompting me password , when enter password , press enter key continues execution , giving me successful message.

now want same thing in java programatically using runtime.execute(...); , working fine till prompting password(i reading output of command prompt) don't know how show dialog box when cmd output prompting password can enter password , that password program continues execution , complete successfully. want same thing doing manually command prompt.

so question is possible enter parameters in between of command execution runtime.execute , how resume command execution passing theat paramenter if if yes how ?

it simple swing application executing command , want use swing ui dialog taking password.

updated code suggested andy thomas :

process p1 =runtime.getruntime().exec("cmd /c 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 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.

when call runtime.exec(), process object representing child process.

  1. read child process's output stream -- provided process.getinputstream() -- until detect request password.
  2. ask user password, preferably using jpasswordfield password hidden.
  3. feed password ot process's input stream, provided process.getoutputstream().
  4. keep reading child process's output stream, or child process may block.

edit: here's sketch (not tested) of how write string userpassword child process, requested comment below. ... elides part reads child process's output stream, without child 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 op     child_writer.flush(); 

Comments