我自己写了个me.bat,命令如下
echo hello every body
pause
然后写了类,main方法如下:public static void main(String args[]){
String bat = "E:\\Project\\TrunkJava\\me.bat";
Process p;
try {
p = Runtime.getRuntime().exec(bat);
InputStream in = p.getInputStream();
int c;
while ((c = in.read()) != -1)
{
System.out.print(c);
}
in.close();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("Done");
}
我的环境是eclipse和XP,我所期望的是,一个dos窗口跳出来现实hello……,等我按任意键后,dos窗口消失,继续执行java代码在eclipse的输出窗口显示done,我不知道我写的代码对不对,执行后eclipse输出一些数字,且永远都不停止,没有dos窗口和done的输出,请教下原因!
echo hello every body
pause
然后写了类,main方法如下:public static void main(String args[]){
String bat = "E:\\Project\\TrunkJava\\me.bat";
Process p;
try {
p = Runtime.getRuntime().exec(bat);
InputStream in = p.getInputStream();
int c;
while ((c = in.read()) != -1)
{
System.out.print(c);
}
in.close();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("Done");
}
我的环境是eclipse和XP,我所期望的是,一个dos窗口跳出来现实hello……,等我按任意键后,dos窗口消失,继续执行java代码在eclipse的输出窗口显示done,我不知道我写的代码对不对,执行后eclipse输出一些数字,且永远都不停止,没有dos窗口和done的输出,请教下原因!
public static void main(String args[]){
String bat = "E:\\Project\\TrunkJava\\me.bat";
Process p;
try {
p = Runtime.getRuntime().exec("cmd.exe /c "+ "start /min E:\\Project\\TrunkJava\\me.bat");
InputStream in = p.getInputStream();
int c;
while ((c = in.read()) != -1)
{
System.out.print(c);
}
in.close();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("Done");
}
这样就能运行,但是我还是想知道为什么?
要先执行cmd.exe 然后执行 你的bat . 我上面的答案就是这个思路.. 但是我那个方法好像不行..这个行了就好了.