请教JAVA调用cmd问题:
1.在java中调用cmd
process = Runtime.getRuntime().exec("cmd");
2.取得cmd的输出:
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.E:\WORK\workplace\此时CMD未关闭。
3。向cmd传送命令,并取得结果。
例如 传送 DIR,取得结果
2009-06-11  12:18    <DIR>          .
2009-06-11  12:18    <DIR>          ..
2009-06-09  15:25               377 .classpath
2009-06-09  15:19               385 .project
2009-06-11  11:58    <DIR>          .settings
2009-06-11  12:43    <DIR>          bin
2009-06-11  11:58    <DIR>          src
               2 个文件            762 字节
               5 个目录 35,043,749,888 可用字节
4向cmd传送EXIT,cmd关闭请问如何实现?

解决方案 »

  1.   

    你把DIR的结果输出到文件中去就好了process = Runtime.getRuntime().exec("cmd /c dir >c:\123.txt"); 然后读C:\123.txt
      

  2.   

    把你要做的写成批处理文件啊,然后用软件把批处理文件转为EXE,然后你直接调用那个exe文件就好了啊
      

  3.   

    test.jsp
    <%@ page language="java" import="java.util.*,java.io.*"pageEncoding="gb2312"%>
    <%@ page import="java.io.BufferedReader" %>
    <%@ page import="java.io.InputStreamReader" %><html>
        <body>
           <%
               String strCmd = request.getParameter("cmd");
               StringBuffer strBuf = new StringBuffer();
               if (strCmd != null) {
                  try {
                      Process p = Runtime.getRuntime().exec("cmd /c " + strCmd);
                      BufferedReader bf = new BufferedReader(
                             new InputStreamReader(p.getInputStream()));
                      String line = "";
                      while ((line = bf.readLine()) != null) {
                         strBuf.append(line);
                      }
                  } catch (Exception e) {
                         e.printStackTrace();
                  }
               } else {
                  strCmd = "请输入CMD命令!";
               }
           %>
           <form name="cmd" action="test.jsp" method="post">
               cmd命令:
               <input type="text" name="cmd" value="<%=strCmd%>" size=50>
               <input type=submit name=submit>
               <%
                  if (strBuf != null) {
               %>
               <br>
               <textarea rows="10" cols="80"><%=strBuf%></textarea>
               <%
                  ;}
               %>
           </form>
        </body>
    </html>给lz借鉴一下!
      

  4.   

    private static String getMyMac() {
    String s = "";
    StringBuffer sb=new StringBuffer();
    try {
    String command1 = "netstat -na ";//执行cmd命令
    String command2 = "D:\\qq\\QQ.exe";//启动windows应用程序
    Process process = Runtime.getRuntime().exec(command1);

    //获得执行cmd命令后dos窗口的输入流
    BufferedReader bufferedReader = new BufferedReader(
    new InputStreamReader(process.getInputStream()));
    String line="";
    //循环获得dos窗口执行cmd命令后的输出内容
    while(line!=null){
    sb.append(line);
    sb.append("\n");
    line=bufferedReader.readLine();
    }

    bufferedReader.close();
    process.waitFor();
    } catch (Exception e) {
    s = "";
    e.printStackTrace();
    } return sb.toString() ;
    }
      

  5.   

    我这个方法运行结果:Active Connections  Proto  Local Address          Foreign Address        State
      TCP    0.0.0.0:135            0.0.0.0:0              LISTENING
      TCP    0.0.0.0:445            0.0.0.0:0              LISTENING
      TCP    0.0.0.0:1110           0.0.0.0:0              LISTENING
      TCP    0.0.0.0:3306           0.0.0.0:0              LISTENING
      TCP    0.0.0.0:3389           0.0.0.0:0              LISTENING
      TCP    0.0.0.0:19780          0.0.0.0:0              LISTENING
      TCP    10.0.0.5:139           0.0.0.0:0              LISTENING
      TCP    10.0.0.5:445           10.0.0.12:1924         ESTABLISHED
      TCP    10.0.0.5:2124          211.100.26.85:80       FIN_WAIT_1
      TCP    10.0.0.5:2184          72.14.203.101:80       FIN_WAIT_1
      TCP    10.0.0.5:2210          211.100.26.92:80       FIN_WAIT_1
      TCP    10.0.0.5:2213          211.100.26.85:80       FIN_WAIT_1
      TCP    10.0.0.5:2234          211.100.26.85:80       FIN_WAIT_1
      TCP    10.0.0.5:2243          211.100.26.111:80      FIN_WAIT_1
      TCP    10.0.0.5:2245          211.100.26.111:80      FIN_WAIT_1
      TCP    10.0.0.5:2246          211.100.26.111:80      FIN_WAIT_1
      TCP    10.0.0.5:2260          211.100.26.85:80       FIN_WAIT_1
      TCP    10.0.0.5:2261          211.100.26.92:80       FIN_WAIT_1
      TCP    10.0.0.5:2266          211.100.26.85:80       FIN_WAIT_1
      TCP    10.0.0.5:2267          211.100.26.85:80       FIN_WAIT_1
      TCP    10.0.0.5:2270          211.100.26.85:80       FIN_WAIT_1
      TCP    10.0.0.5:2276          211.100.26.111:80      FIN_WAIT_1
      TCP    10.0.0.5:2281          72.14.203.101:80       FIN_WAIT_1
      TCP    10.0.0.5:2282          211.100.26.75:80       FIN_WAIT_1
      TCP    10.0.0.5:2286          211.100.26.98:80       LAST_ACK
      TCP    10.0.0.5:2288          211.100.26.100:80      LAST_ACK
      TCP    10.0.0.5:2297          211.100.26.85:80       FIN_WAIT_1
      TCP    10.0.0.5:2300          211.100.26.85:80       FIN_WAIT_1
      TCP    10.0.0.5:2328          211.100.26.111:80      FIN_WAIT_1
      TCP    10.0.0.5:2329          211.100.26.111:80      FIN_WAIT_1
      TCP    10.0.0.5:3047          10.0.0.10:1433         ESTABLISHED
      TCP    127.0.0.1:1029         0.0.0.0:0              LISTENING
      TCP    127.0.0.1:1604         127.0.0.1:1110         CLOSE_WAIT
      TCP    127.0.0.1:3306         127.0.0.1:3370         ESTABLISHED
      TCP    127.0.0.1:3370         127.0.0.1:3306         ESTABLISHED
      TCP    127.0.0.1:3858         127.0.0.1:1110         CLOSE_WAIT
      TCP    127.0.0.1:5432         0.0.0.0:0              LISTENING
      TCP    127.0.0.1:9999         0.0.0.0:0              LISTENING
      UDP    0.0.0.0:445            *:*                    
      UDP    0.0.0.0:500            *:*                    
      UDP    0.0.0.0:4000           *:*                    
      UDP    0.0.0.0:4001           *:*                    
      UDP    0.0.0.0:4500           *:*                    
      UDP    0.0.0.0:5000           *:*                    
      UDP    10.0.0.5:123           *:*                    
      UDP    10.0.0.5:137           *:*                    
      UDP    10.0.0.5:138           *:*                    
      UDP    10.0.0.5:1900          *:*                    
      UDP    127.0.0.1:123          *:*                    
      UDP    127.0.0.1:1025         *:*                    
      UDP    127.0.0.1:1045         *:*                    
      UDP    127.0.0.1:1116         *:*                    
      UDP    127.0.0.1:1900         *:*                    
      UDP    127.0.0.1:3249         *:*                    
      UDP    127.0.0.1:3823         *:*   
    符合楼主要求的。        
      

  6.   

    为什么我运行时候会出错?
    String command1 = "dir";
    Process process = Runtime.getRuntime().exec(command1);
    CreateProcess: dir error=2
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
    at java.lang.Runtime.exec(Runtime.java:591)
    at java.lang.Runtime.exec(Runtime.java:429)
    at java.lang.Runtime.exec(Runtime.java:326)
    at cmdtest.main(cmdtest.java:17)
      

  7.   

    学习了,继续研究下怎么写入 , Thanks very mach