@@ -5157,6 +5157,25 @@ String doExt() {
51575157 return desc .dispatch (this );
51585158 }
51595159
5160+ void handleProcessBuffers (InputStream ... streams ){
5161+ for (int i = 0 ; i < streams .length ; i ++) {
5162+ final InputStream is = streams [i ];
5163+ new Thread (new Runnable (){
5164+ @ Override
5165+ public void run (){
5166+ try {
5167+ BufferedReader br = new BufferedReader (new InputStreamReader (is ));
5168+ while (br .readLine () != null )
5169+ {}
5170+ } catch (IOException ioe )
5171+ {
5172+ ioe .printStackTrace ();
5173+ }
5174+ }
5175+ }).start ();
5176+ }
5177+ }
5178+
51605179 String exec () {
51615180 String [] cmd ;
51625181 StringBuffer sb = new StringBuffer (256 );
@@ -5191,14 +5210,20 @@ String exec() {
51915210 Process p = Runtime .getRuntime ().exec (cmd );
51925211 boolean returnImmediately = openingDoc || !waitForCompletion ;
51935212 waitForCompletion = true ;
5194- if (returnImmediately )
5213+ if (returnImmediately ){
5214+ handleProcessBuffers (p .getInputStream (), p .getErrorStream ()); // empty both buffers
51955215 return null ;
5196- reader = new BufferedReader (new InputStreamReader (p .getInputStream ()));
5216+ } else {
5217+ handleProcessBuffers (p .getErrorStream ()); // empty only unused ErrorStream buffer
5218+ }
5219+ reader = new BufferedReader (new InputStreamReader (p .getInputStream ())); // notice that this function will discard all process error output
51975220 String line ; int count =1 ;
51985221 while ((line =reader .readLine ())!=null ) {
51995222 sb .append (line +"\n " );
5200- if (count ++==1 &&line .startsWith ("Microsoft Windows" ))
5201- break ; // user probably ran 'cmd' without /c option
5223+ if (count ++==1 &&line .startsWith ("Microsoft Windows" )){
5224+ handleProcessBuffers (p .getInputStream ()); // must empty the InputStream buffer anyway
5225+ break ; // user probably ran 'cmd' without /c option
5226+ }
52025227 }
52035228 } catch (Exception e ) {
52045229 sb .append (e .getMessage ()+"\n " );
0 commit comments