Execute external command and obtain the result : Process « Development « Java Tutorial






import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
  public static void main(String[] args) throws Exception{
    Process process = Runtime.getRuntime().exec("ls -al");
    process.waitFor();

    int exitValue = process.exitValue();
    System.out.println("exitValue = " + exitValue);
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = "";
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }
  }
}








6.27.Process
6.27.1.Reading Output from a Command
6.27.2.Sending Input to a Command
6.27.3.Execute external command and obtain the result
6.27.4.Helper method to execute shell command