Process me=Runtime.getRuntime().exec("ping 202.204.120.68");
BufferedReader mein=new BufferedReader(new InputStreamReader(me.getInputStream()));
String c;
while(c=mein.readLine())!=null
{
out.println(c+"<br>");
}
这是ping 我的ip ,中可以在IE中显示

解决方案 »

  1.   

    这个ping程序太简单了
    我的console程序比较复杂,运行它后还要和它交互:
    smart.exe inter spec0run中国
    .
    q
      

  2.   

    谁知道怎么在JSP页面里启动Java Web Start
      

  3.   

    晕,同意MasterChen(金甲圣士)的
    这个ping也没什么复杂的啊代为注释:
    //下面这句调用Runtime类,这个类可以得到jvm的相关参数,getRuntime()是静态方法
    //exec命令就是执行一个外部程序.exec(String programname)
    Process me=Runtime.getRuntime().exec("ping 202.204.120.68");//BufferedReader是java.io中的类,可以得到文本输入
    //me.getInputStream就是把exec执行的命令的显示作为Input流,
    //然后先用InputStreamReader读取
    BufferedReader mein=new BufferedReader(new InputStreamReader(me.getInputStream()));String c;
    //mein.readLine()就是从输出中读取一行
    while(c=mein.readLine())!=null
    {
    out.println(c+"<br>");
    }
      

  4.   

    晕。。看错问题了,当我没说吧~~如果要交互就是在exec()的时候加入命令参数呗,
    得到的结果,用一个分析器来包装如果结果是固定的,每行代表固定意思就容易了
    既然有了inputstream就可以处理每一行的结果了
    如果是动态的,还需要在每一行作个关键字来标示,判断每行是干吗的
      

  5.   

    比如说执行linux上的passwd命令,如果在控制台上操作需要交互重复输入密码,这时在JAVA中该如何写呢?
      

  6.   

    谢谢 icecloud(冰云) 的解答。
    请回答一下zhaofj(叶三耿)的问题吧。
    我的问题其实和他的类似。麻烦贴出代码,谢谢了。
      

  7.   

    Runtime.getRuntime().exec(args,env,pathname)
    env是environment,pathname是路径。
      

  8.   

    那么怎么样调出显示DOS下ping命令的执行呢?
    而不是在系统内部执行
    也就是说打开一个CMD命令窗口
    像平常在DOS下运行的一样
      

  9.   

    to:migrant1119(候鸟)
    能否举例说明一下你的代码,让俺们菜鸟类学学
      

  10.   

    看看这段有没有帮助:
    Runtime.class API
     Process exec(String command) 
              Executes the specified string command in a separate process. 
     Process exec(String[] cmdarray) 
              Executes the specified command and arguments in a separate process. 
     Process exec(String[] cmdarray, String[] envp) 
              Executes the specified command and arguments in a separate process with the specified environment. 
     Process exec(String[] cmdarray, String[] envp, File dir) 
              Executes the specified command and arguments in a separate process with the specified environment and working directory. 
     Process exec(String cmd, String[] envp) 
              Executes the specified string command in a separate process with the specified environment. 
     Process exec(String command, String[] envp, File dir) 
              Executes the specified string command in a separate process with the specified environment and working directory. Process.class API
    abstract  InputStream getErrorStream() 
              Gets the error stream of the subprocess. 
    abstract  InputStream getInputStream() 
              Gets the input stream of the subprocess. 
    abstract  OutputStream getOutputStream() 
              Gets the output stream of the subprocess. 
      

  11.   

    我想,既然exec返回的Process对象有Input,Output和Error
    那么就足够判断这个人是否登陆完成了吧?或许还能得到他输入的密码@_@没试过,仅是想法
      

  12.   

    SUN-cn提供的FAQ
    问: 
    为什么Runtime.exec("ls")没有任何输出? 答: 调用Runtime.exec方法将产生一个本地的进程,并返回一个Process子类的实例,该实例可用于控制进程或取得进程的相关信息. 由于调用Runtime.exec方法所创建的子进程没有自己的终端或控制台,因此该子进程的标准IO(如stdin,stdou,stderr)都通过Process.getOutputStream(),Process.getInputStream(), Process.getErrorStream()方法重定向给它的父进程了.用户需要用这些stream来向 子进程输入数据或获取子进程的输出. 所以正确执行Runtime.exec("ls")的例程如下: try 

    process = Runtime.getRuntime().exec (command); 
    InputStreamReader ir=newInputStreamReader(process.getInputStream()); LineNumberReader input = new LineNumberReader (ir); String line; 
    while ((line = input.readLine ()) != null) System.out.println(line); 
    } catch (java.io.IOException e){ 
    System.err.println ("IOException " + e.getMessage());