// Execute2.java - Execute a Windows command
// This version works in Java 1.5 and beyond

import java.io.*;
import java.util.Scanner;


public class Execute2 {

    public Execute2() {
        execute("cmd /C dir /W");
        execute("cmd /C echo one two three");
        execute("cmd /C Notepad dataFile.txt");
        execute("cmd /C echo .");
    } // end Execute2()

    private void execute(final String cmd) {
        try {
            Process process = Runtime.getRuntime().exec(cmd); 		
            Scanner scan = new Scanner(process.getInputStream());    		    		
			// Retrieve output from command execution
            while (scan.hasNext()) { 
                System.out.println(scan.nextLine());
            }
        } catch (IOException e) {    		
            System.out.println(e.getMessage());
        } 
    } // end execute()
	
    public static void main(String[] args) {
        new Execute2();
    } // end main()

} // end class Execute2
