/** * RunACommand tests the exec() method of Runtime and the Process class * * @author: Leif E. Andersen, BLA*net * */ import java.io.*; public class RunACommand { public static void main (String arg[]) throws Exception { // obtain the runtime environment of myself Runtime me = Runtime.getRuntime() ; System.out.println ("My environment is "+me); // execute a command Process cmd = me.exec("ls"); System.out.println ("The command is exec'ed in " + cmd); BufferedReader inp = new BufferedReader( new InputStreamReader(cmd.getInputStream())); // print out current output of cmd // Is InputStream for me-process System.out.println("and output of the command is as follows:"); String line; while ((line = inp.readLine())!= null) { System.out.println("-> "+ line); } } }