Java调CMD进程 关闭不了该CMD进程,求解决~~~~~~~~~~~~~
Process pro = Runtime.getRuntime().exec("CMD /C START");pro.waitFor();
pro.destroy();
无作用,该cmd窗口还是在那,但是打开记事本
Process pro = Runtime.getRuntime().exec("notepad.exe");
pro.waitFor();//程序阻塞
pro.destory();//程序关闭
为什么CMD就不行,Java怎么控制这个CMD进程,求高手解答~~~~~~~ --------------------编程问答--------------------
--------------------编程问答-------------------- start后面为什么不放东西呢,应该是另一个程序啊,如
/**
* CSDN-测试Process
* @author WangYanCheng
* @version 2012-2-25
*/
public class CMDProcess {
/**一个外部进程*/
private String commandOne = "CMD /C notepad";
/**这里我理解的是会有两个外部进程的概念,但不确定*/
private String commandTwo = "CMD /C START /WAIT NOTEPAD";
/**
* doWork
*/
public void doWork() {
int flag = -1;
try {
Process procInst = Runtime.getRuntime().exec(commandTwo);
flag = procInst.waitFor();
if (flag == 0) {
System.out.println("安全退出==>" + flag);
}
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 测试入口
* @param args 参数列表
*/
public static void main(String[] args) {
new CMDProcess().doWork();
}
}
cmd.exe /c start javac.exe xxx.java
--------------------编程问答--------------------
Process pro = Runtime.getRuntime().exec("CMD /C START notepad");
pro.waitFor();
pro.destroy();
Process pro = Runtime.getRuntime().exec("CMD /C START cmd"");
pro.waitFor();
pro.destory();
我得意思是:Java打开记事本窗口会等待会关闭,Java打开cmd窗口就不行,waitFor()没作用,destory也关闭不了 --------------------编程问答-------------------- 楼主3楼的代码我这里效果没区别的啊,
执行后,
主线程都阻塞,
都等待用户关闭窗口。(notepad窗口或cmd窗口) --------------------编程问答-------------------- 我也在找解决方法,根据JDK API上的说法,destory()方法对CMD窗口无效···下面是API上的原文:
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts。
参考链接:http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html --------------------编程问答--------------------
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Process pr = Runtime.getRuntime().exec("CMD /C START notepad");
pr.waitFor();
pr.destroy();
}
我只弹出了记事本程序,也没有等待会关闭。
Process pr = Runtime.getRuntime().exec("CMD /C START CMD");
改成上句后,弹出dos窗口。也没有关闭。
这个是什么意思呢??
--------------------编程问答--------------------
这会明白了,pr.destroy();关闭不了cmd。
如果按2楼的方法,加载了另一个程序。也关闭不了。而且进程里有cmd进程。。
Process pr = Runtime.getRuntime().exec("CMD /c start ping localhost");
这样能关闭了。。。是不是用cmd调用其他程序,只能等人工处理完毕关闭?
补充:Java , Java SE