//package src;

// Java World -- Fueling Innovation
// When Runtime.exec() won't -- Michael C. Daconta 
// http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
// December 2004
// IDG

import java.util.*;
import java.io.*;

public class TestExec
{
    public static int runCmd (String args[])
    {
		int exitVal = -1;
		
    	/*
        if (args.length < 1)
        {
            System.out.println("USAGE: java runCmd \"cmd\"");
            System.exit(1);
        }
        */
        
        try
        {
            String cmd = args[0];
            Runtime rt = Runtime.getRuntime();
				System.out.println("exec(): " + cmd);
            Process proc = rt.exec(cmd);
            
            // any error message?
            StreamGobbler errorGobbler = new 
                StreamGobbler(proc.getErrorStream(), "ERR");            
            
            // any output?
            StreamGobbler outputGobbler = new 
                StreamGobbler(proc.getInputStream(), "OUT");
                
            // kick them off
            errorGobbler.start();
            outputGobbler.start();
                     
            // any error???
            exitVal = proc.waitFor();
            
        } catch (Throwable t)
          {
            t.printStackTrace();
        }
          
		return exitVal;
    }
}
