【问题标题】:code Not Running [closed]代码未运行[关闭]
【发布时间】:2012-07-03 16:57:27
【问题描述】:

我在使用此代码时遇到运行时异常

Runtime rt = Runtime.getRuntime();
System.out.println("Executing " + "uname -a");
Process x = rt.exec("ping IP Address of system");
BufferedReader stdInput =
    new BufferedReader(new InputStreamReader(x.getInputStream()));
BufferedReader stdError =
    new BufferedReader(new InputStreamReader(x.getErrorStream()));
String line = null;
while ((line = stdInput.readLine()) != null) {
    System.out.println(line);
}
x.waitFor();        

【问题讨论】:

  • 您到底遇到了什么异常?您对此有何疑问?
  • 你能粘贴异常堆栈跟踪吗?
  • 我在 Windows 7 64bit 上试过,效果很好。
  • 问题是什么? uname -aping 应该是一样的吗?
  • When Runtime Process exec won't。正如其他人所说,更多信息会很好。

标签: java bufferedreader


【解决方案1】:

试试这个方法..

public class PingClass {

    public void pingIt(){

        InetAddress addr;
        try {
            String line;

            Process p = Runtime.getRuntime().exec(
                    "C:/windows/system32/ping.exe 192.168.2.2");

            /**
             * Create a buffered reader from the Process' input stream.
             */
            BufferedReader input = new BufferedReader(new InputStreamReader(p
                    .getInputStream()));

            /**
             * Loop through the input stream to print the program output into console.
             */
            ArrayList<String> str = new ArrayList<String>();
            while ((line = input.readLine()) != null) {
                str.add(line);
                System.out.println(line);

            }
            /**
             * Finally close the reader
             */
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   


    public static void main(String[] args){


        new PingClass().pingIt();
    }

}

【讨论】:

    猜你喜欢
    • 2017-07-06
    • 2017-12-19
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多