拿去试一试:import java.io.*;public class RunPing {
  public static void main(String[] args) {
    Process process;
    try {
      process = Runtime.getRuntime().exec("ping 127.0.0.1");
      InputStream in = process.getInputStream();
      BufferedReader reader = new BufferedReader(
              new InputStreamReader(in));
      String line = "";
      while ((line = reader.readLine()) != null) {
        System.out.println(line);
      }
      in.close();
      reader.close();
      process.destroy();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

解决方案 »

  1.   

    以Unix 为例, DOS 或 Window 系统相似: 
    import java.io.*;
    import java.util.*;class IoRedirect {
    public static void main(String Argv[]) {
    try {
    String[] command = {"/bin/sh", "-c", "/bin/ls > out.dat"};
    Process p = Runtime.getRuntime().exec(command);
    p.waitFor();
    System.out.println("return code: " + p.exitValue());
    } catch (IOException e) {
    System.err.println("IO error: " + e);
    } catch (InterruptedException e1) {
    System.err.println("Exception: " + e1.getMessage());
    }
    }
    }