/* * ExecPgm.java * ------------ * * Descricao: Executa programa passado como argumento na linha de * comando e captura a saida da execucao do comando de * stdout caso o programa tenha terminado sem erro ou * de stderr caso o programa tenha terminado anormalmente * veja metodo: Process.waitFor() * * Exemplo: java ExecPgm ecoa alo .... * * O programa dos ecoa.c exibe os parametros recebidos na * linha de comando e termina NORMALMENTE quando o numero * de argumentos for par e ANORMALMENTE quando for impar * veja ecoa.c * * @author ismael h f santos - Created on 22 de junho de 2002 * * @email ismael@tecgraf.puc-rio.br */ import java.io.*; public class ExecPgm { public static void main( String args[] ) { // get the runtime object associated with the current Java application. Runtime r = Runtime.getRuntime(); // Executes the specified command and arguments in a separate process // Example: java ExecPgm ecoa .... if( args.length > 0 ) { PrintWriter out = new PrintWriter( System.out, true ); out.print("==> Executing command:" ); for(int i=0; i NORMAL termination retCode=" + ret ); while( true ) { if( (bytesRead=fromPgm.read(result)) == -1 ) break; out.write(result, 0, bytesRead); } } else { out.println("==> ABNORMAL termination retCode=" + ret ); while( true ) { if( (bytesRead=fromErrPgm.read(result)) == -1 ) break; out.write(result, 0, bytesRead); } } out.flush(); } catch( InterruptedException e ) { e.printStackTrace(); } catch( IOException e ) { e.printStackTrace(); } } } }