还是不行,我仅写String command="cmd";的时候,只能调出dos下的黑色的窗口,并没有出现dos命令的提示行,这是什么原因?

解决方案 »

  1.   

    try{
          String command = "notepad";
          Process child = Runtime.getRuntime().exec(command);
          if(child.waitFor()==0)
          {
            System.out.println("Process finished!");
          }
          else
          {
            System.out.println("Process terminated!");
          }
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
        }
    这样反正是可以的,你改改吧。
    不过你的命令应该没有写错,所以应该是能够启动程序的才对。但如果用child.exitValue();而你调用的程序又没有结束的话会抛异常!
      

  2.   

    确实能调用notepad,但是改成其他的DOS应用程序就不对了,这是为什么?
      

  3.   

    import java.io.*;class exectest
    {
      public static void main(String[] args)
      {
        try {
          String cmd = "cmd /c dir f:";
          Process child = Runtime.getRuntime().exec(cmd);
          InputStream child_in = child.getInputStream() ;
          BufferedReader inbr=new BufferedReader(new InputStreamReader(child_in));      String line;
          line=inbr.readLine();
          while (line!=null)
          {
            System.out.println(line);
            line=inbr.readLine();
          }
          child_in.close();
          child.destroy() ;
        } catch (IOException e) {
          System.err.println(e);
        }
      }
    }
    win2000+jbt通过