java - Send Parameter To Runtime.getRuntime().exec() After Execution -
i need execute command in java program after executing command , required parameter ( password in case ). how can manage output process of runtime.getruntime().exec()
accept parameter further execution ?
i tried new bufferedwriter(new outputstreamwriter(signingprocess.getoutputstream())).write("123456");
did not work.
does program not feature --password option ? command line based programs do, scripts.
runtime.getruntime().exec(new string[]{"your-program", "--password="+pwd, "some-more-options"});
or more complicated way , more error-prone:
try { final process process = runtime.getruntime().exec( new string[] { "your-program", "some-more-parameters" }); if (process != null) { new thread(new runnable() { @override public void run() { try { datainputstream in = new datainputstream( process.getinputstream()); bufferedreader br = new bufferedreader( new inputstreamreader(in)); string line; while ((line = br.readline()) != null) { // handle input here ... -> // if(line.equals("enter password:")) { ... } } in.close(); } catch (exception e) { // handle exception here ... } } }).start(); } process.waitfor(); if (process.exitvalue() == 0) { // process exited ... } else { // process failed ... } } catch (exception ex) { // handle exception }
this sample opens new thread (keep in mind concurrency , synchronisation) that's going read output of process. similar can feed process input long has not terminated:
if (process != null) { new thread(new runnable() { @override public void run() { try { dataoutputstream out = new dataoutputstream( process.getoutputstream()); bufferedwriter bw = new bufferedwriter( new outputstreamwriter(out)); bw.write("feed process data ..."); bw.write("feed process data ..."); out.close(); } catch (exception e) { // handle exception here ... } } }).start(); }
hope helps.
Comments
Post a Comment