Java执行本地命令

Eave 2016.08.10 17:38
try
{
    ProcessBuilder builder = new ProcessBuilder();
    builder.redirectErrorStream(true);
    Process process = builder.command("ls", "-la").start();

    InputStream is = process.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF8"));
    String line = null;
    while ((line = br.readLine()) != null)
    {
        System.out.println(line);
    }
}
catch(Exception e)
{}
try
{
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec("ls -la");
    InputStream is = process.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF8"));
    String line = null;
    while((line = br.readLine()) != null)
    {
       System.out.println(line);
    }
}
catch(Exception e)
{}