如何执行BAT文件?Runtime.getRuntime().exec()是执行EXE的,BAT不行了```

解决方案 »

  1.   

    cmd.exe 环境指明了吗 ,bat是在当前路径吗 ?
      

  2.   

    public static void main(String[] args)
    {try
    {
    Runtime.getRuntime().exec("C:\\aa.bat");}
    catch (Exception ex)
    {
    ex.printStackTrace();
    }
    }
    还是不行,太多人认为Runtime.getRuntime().exec()可以执行BAT了,请高手指点一下吧。
      

  3.   

    Runtime.getRuntime().exec("cmd.exe /c aa.bat");
    have a try
      

  4.   

    Runtime.getRuntime().exec("cmd.exe /c aa.bat");
    试了,不行。
      

  5.   

    要写绝对路径的,如果aa.bat的文件夹不在PATH里面的话。
      

  6.   

    我前几天刚做过的,100%能行。
    下面是我实现的类,你用时Command com=new Command("exam.bat")便可以。
    public class Command
    {
      String cmd;
        public Command(String cmd) 
       {
         this.cmd=cmd;
         try 
         {
          Process child = Runtime.getRuntime().exec(cmd);
          InputStream in = child.getInputStream();
          int c;
          while ((c = in.read()) != -1)//这语句不能少,只有读了才能执行
          {
    //System.out.print(c);
          }
          in.close();
          try
          {
    child.waitFor();
          }
          catch (InterruptedException e) 
          {
    e.printStackTrace();
          }
         }
         catch (IOException e)
         {
    e.printStackTrace();
         }
       }
    }
      

  7.   

    http://blog.csdn.net/gavin_sw/archive/2007/01/22/1489657.aspx
      

  8.   

    public class Test {    public static void main(String[] args) {
           
            String s = "cmd.exe /c start /min " + "c:\\test.bat";
            try {
            Runtime.getRuntime().exec(s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }}