java调用bat

来源:互联网 发布:python运维开发工程师 编辑:程序博客网 时间:2024/06/18 16:46

java调用bat方法一二


1. 方案1

import java.io.IOException;import java.io.InputStream;public class runbat {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString cmd = "cmd /c start D:\\xml\\code.bat"; //passtry {Process ps = Runtime.getRuntime().exec(cmd);ps.waitFor();} catch (IOException ioe) {ioe.printStackTrace();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("child thread done");}}


1.1 缺点

会弹出cmd框,并且不能自动关闭


1.2 解决方法

在bat文件最后加上

exit

#当前目录set curdir=%~dp0java -classpath "%curdir%code1.jar" test.code1.UserInfoControllerexit

1.3 能够自动退出,但依然存在问题

即使能够自动退出,但是每次调用这个bat的时候屏幕总是会闪一下cmd命令框。


2.方案2

import java.io.IOException;import java.io.InputStream;public class runbat {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString batName = "D:\\xml\\code.bat"; //passtry {Process ps = Runtime.getRuntime().exec(batName);InputStream in = ps.getInputStream();int c;while ((c = in.read()) != -1) {System.out.print(c);// 如果你不需要看输出,这行可以注销掉}in.close();ps.waitFor();} catch (IOException ioe) {ioe.printStackTrace();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("child thread done");}}

完!


http://www.cnblogs.com/xwdreamer/archive/2011/12/12/2296911.html


0 0
原创粉丝点击