[Java]Java调用.bat文件

0 注意

  1. java的runtime不会加载系统的环境变量,需要自己手动设置,在.bat文件中
set path=C:\Program Files\R\R-3.4.2\bin
  1. java调用.bat时,当前路径是jar包上级目录或者是项目的根目录

1 出现cmd窗口

          public static void runbat(String batPath) {
            String cmd = "cmd /c start "+ batPath ;// pass
            try {
                Process ps = Runtime.getRuntime().exec(cmd);
                ps.waitFor();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("child thread donn");
        }

2 不出现cmd窗口

         public static void runBatWithoutCmd(String batPath) {
          try {
                Process ps = Runtime.getRuntime().exec(batPath);
                InputStream in = ps.getInputStream();
                int c;
                while ((c = in.read()) != -1) {
                }
                in.close();
                ps.waitFor();

            } catch (IOException ioe) {
                ioe.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
      }

你可能感兴趣的:([Java]Java调用.bat文件)