Executing native commands from Java

package subin.rnd.nativity;
import java.io.IOException;
import java.io.InputStream;
public class NativityTest
{
    public static void main(String[] args)
    {
        String cmd = "cmd /c dir";
        _runtime = Runtime.getRuntime();
        executeCommand(cmd);
    }
    
    private static Runtime _runtime;
    private static Process _process; 
    private static InputStream _inStream;
    
    private static void executeCommand(String cmd)    
    {
        System.out.println("Executing > " + cmd);
        
        _inStream = null;
        _process = null;
        int ch;
        
        try
        {
            _process = _runtime.exec(cmd);
            _inStream = _process.getInputStream(); 
            while ((ch = _inStream.read()) != -1)
            {
                System.out.print((char) ch);
            }
        }
        catch (IOException e)
        {
            System.out.println("Exception");
        }
        finally
        { 
            try
            {
                if (_process != null) _process.destroy();
                if (_inStream != null) _inStream.close();
            }
            catch (Exception e){}
        }
    }    
}
Advertisements