Process ps = Runtime.getRuntime().exec("dir");
dir 应该就不能执行,不是系统命令!

解决方案 »

  1.   

    Process ps = Runtime.getRuntime().exec("NotePad.exe");应当是系统的可执行文件才行吧?dir 在Linux下是有存在在Win中command里面的常驻命令啊
      

  2.   

    把Process ps = Runtime.getRuntime().exec("dir");
    改成
    Process ps = Runtime.getRuntime().exec("cmd /c \"dir C:\"");  //列C盘根目录
      

  3.   

    感谢大家的回答 你们的答案非常好! 我今天也查到了dir在2000 xp 上不是程序 谢谢大家 分数照加 下面我还想提出个问题在此一起讨论
      就是是否不用jni也可以和控制台进行数据交互处理呢? 
      Runtime 能否作到呢? 再一次的感谢大家 我会尽快的结贴
      

  4.   

    newsoldier(void) 
    Process ps = Runtime.getRuntime().exec("cmd /c \"dir C:\"");  //列C盘根目录
    why couldnt to run
      

  5.   

    To:pqds(一个女农民)
    应当是:
    Process ps = Runtime.getRuntime().exec("cmd /C \"dir C:\\\"");
    注意转义符的使用!!!C:\>cmd/?
    启动 Windows 2000 命令解释器一个新的实例/C      执行字符串指定的命令然后终断
      

  6.   

    Process ps = Runtime.getRuntime().exec(String[] strs)String[] cmd = { "cmd","/C","dir" };Process ps = Runtime.getRuntime().exec(cmd);这样就可以了
      

  7.   

    phoow(凤凰) (
    请测试一下先
      

  8.   

    我的测试程序
    import java.io.BufferedInputStream;
    import java.io.*;public class TestCMD {
      static  InputStream in;
      static public void main(String[] args) {
       
       TestCMD t=new TestCMD();
        t.Running();

      }
    // read an input-stream into a String
      void Running(){
      
      
       Runnable read=new Runnable(){
           public void run(){
              try{
               Thread.sleep(604);//60 or bigger!
              }catch(Exception e){
              System.out.println("read is erro ");
              }         
             
         String strs="";
      TestCMD.in = new BufferedInputStream(TestCMD.in);
      StringBuffer buffer =new StringBuffer();
          BufferedReader input=new BufferedReader(new InputStreamReader(TestCMD.in));
     try{
      String str=input.readLine();
      while(str!=null){
       strs=strs+str+"\n"; 
       System.out.println(str);
           str=input.readLine();
          }
         }catch(Exception e){
          System.out.print("");
         }
         
         System.out.println("read is end ");
       }
      
         };
        
      
       Runnable write=new Runnable(){
             public void run(){
                              
       String[] cmd = { "cmd","/C","dir" };
    try {
    Runtime run= Runtime.getRuntime();
       Process ps = run.exec(cmd);
    TestCMD.in=ps.getInputStream();
    } catch(IOException ioe) {
    ioe.printStackTrace();
        }
        System.out.println("write is end");
           }
          public void start1(){
            
            System.out.print("write");
          }
    };
    Thread r=new Thread(read);
    Thread w=new Thread(write); r.start();
    w.start();
      }
    }
    =====================
    反馈:
    write is end
     驱动器 C 中的卷没有标签。
     卷的序列号是 9CE1-02BD C:\Documents and Settings\Administrator\桌面 的目录2003-09-08  18:05    <DIR>          .
    2003-09-08  18:05    <DIR>          ..
    2003-09-10  11:06               422 JCreator Pro.lnk
    2003-09-10  11:07             1,492 Java Web Start.lnk
    2003-01-09  19:51        76,016,799 eclipse-202.exe
    2002-11-15  20:04    <DIR>          eclipse
    2003-08-13  20:46         3,934,731 lomboz.211.zip
    2003-09-10  10:33    <DIR>          lomboz.211
    2003-03-15  08:00         1,418,074 fg140.exe
    2003-09-10  10:57                 0 CUSTDATA.INI
    2003-09-10  10:57               576 FlashGet.lnk
    2003-08-13  18:16            27,661 oo.zip
    2003-09-10  12:16    <DIR>          eclipse-2.1
    2003-09-10  12:18        64,775,326 eclipse-2.1.zip
    2003-09-10  12:29    <DIR>          eclipse-SDK-2.1-win32
    2003-09-10  18:54               539 Java反编译专家 V1.1.lnk
    2003-09-10  19:01                16 copy.bat
    2003-09-10  20:57               546 腾讯QQ.lnk
    2003-09-10  20:57               582 腾讯Explorer.lnk
    2003-09-13  09:27               508 a.lnk
    2003-09-13  09:35             1,217 Test.class
    2003-09-13  09:35               693 Test.java
    2003-09-13  10:25                 0 新建 文本文档.txt
    2003-09-13  10:25                 3 aaa.bat
    2003-09-13  17:34               628 TestCMD.class
    2003-09-13  13:09                 0 JBOSS.log
    2003-09-13  17:34             1,259 TestCMD$1.class
    2003-09-13  17:34             1,065 TestCMD$2.class
    2003-09-13  14:06             1,608 TestCMD.java
                  23 个文件    146,183,745 字节
                   6 个目录 13,794,279,424 可用字节
    read is end
    Press any key to continue...
      

  9.   

    to; pqds(一个女农民)不足: 没有实现动态的交互行为 
       给个意见 
      

  10.   

    利用args[]进行传递参数进来不就得了?
      

  11.   

    to  YuLimin(阿敏当兵) 那不也是静态的么?
    看来 是误会我的意思了
    我是说 在控制台与java程序之间不用jin技术 也要达到交互式的处理效果
      

  12.   

    对不起 是jni技术(即我不想用本地化的东西)