美文网首页
[Java]Java调用.bat文件

[Java]Java调用.bat文件

作者: VincentJianshu | 来源:发表于2018-01-10 18:01 被阅读0次

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文件

      本文链接:https://www.haomeiwen.com/subject/apzonxtx.html