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 stub
String cmd = "cmd /c start D:\\xml\\code.bat"; //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 done");
}
}
会弹出cmd框,并且不能自动关闭
1.2 解决方法
在bat文件最后加上
exit
#当前目录
set curdir=%~dp0
java -classpath "%curdir%code1.jar" test.code1.UserInfoController
exit
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 stub
String batName = "D:\\xml\\code.bat"; //pass
try {
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 block
e.printStackTrace();
}
System.out.println("child thread done");
}
}
完!
http://www.cnblogs.com/xwdreamer/archive/2011/12/12/2296911.html