try {
   Process process = Runtime.getRuntime().exec("ping 10.144.98.100");  //调用Ping命令
  }catch (Exception  e)
        {
            e.printStackTrace();
            }  

解决方案 »

  1.   

    现在调用命令没问题了,但是无法正常输出结果
    代码如下:
    public class ping {    public static void main(String[] args) {
            byte[] pings = new byte[1024*16];
            try {
                Process process = Runtime.getRuntime().exec("ping 127.0.0.1");  //!)用Ping命令
                InputStream in = process.getInputStream();
               
                in.read(pings);
                String tmp = new String(pings);
                tmp = tmp.replaceAll("\r\r\n","");
                System.out.println(tmp);
            }catch (Exception  e){
                e.printStackTrace();
            }
        }
    }
    我用eclipse在windows下调试,信息输出不完全,而且会自动关闭eclipse。直接在liunx下编译的话也会出现信息输出不完全的情况,这是怎么回事啊?
      

  2.   

    用stream来读,好像不太好,一般都用Reader来读:
               InputStream in = process.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String s;
                while ((s = br.readLine())!=null)
                 System.out.println(s);
      

  3.   

    问题解决了,谢谢。
    还有个问题顺路问一下,用哪个包可以把文件压缩成tar.gz?