Read output from a Command execution : Runtime System « Development « Java Tutorial






import java.io.InputStream;

public class Main {
  public static void main(String[] argv) throws Exception {
    String command = "ls";
    Process child = Runtime.getRuntime().exec(command);

    InputStream in = child.getInputStream();
    int c;
    while ((c = in.read()) != -1) {
      System.out.println((char) c);
    }
    in.close();
  }
}








6.47.Runtime System
6.47.1.Milliseconds elapsed since January 1, 1970
6.47.2.Demonstrate totalMemory(), freeMemory() and gc().
6.47.3.Demonstrate exec().
6.47.4.Wait until notepad is terminated.
6.47.5.Timing program execution.
6.47.6.Using arraycopy().
6.47.7.Display the total amount of memory in the Java virtual machine.
6.47.8.Display the maximum amount of memory
6.47.9.Display the amount of free memory in the Java Virtual Machine.
6.47.10.Get Number of Available Processors
6.47.11.System.getProperty
6.47.12.Execute system command
6.47.13.Determine when the application is about to exit
6.47.14.Execute a command from code
6.47.15.Execute a command with more than one argument
6.47.16.Launch a Unix script with Java
6.47.17.Read output from a Command execution
6.47.18.Send an Input to a Command
6.47.19.From Runtime.exec() to ProcessBuilder
6.47.20.Registering Shutdown Hooks for Virtual Machine