如何调用外部exe 程序,但要知到程序是否已结束? try{ Runtime.getRuntime().exec("dir").waitFor();}catch(Exception e){} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 外部exe程序的返回结果可以用Process.exitValue()取得。调用Process.exitValue()时,如果外部exe程序还在运行,则会抛出例外。所以在Java中要用Process.waitFor()来等待外部exe程序的结束。例:public class StatusCode{ public static void main(String argv[]) { Runtime runtime = Runtime.getRuntime(); try{ Process process = runtime.exec("test.exe"); process.waitFor(); System.out.println("Exit Code:" + process.exitValue()); } catch(Exception e){ e.printStackTrace(); } }} 感谢各位, 我也试过,但程序无法向下进行。一直停在waitFor()那Runtime r=Runtime.getRuntime(); Process p=null; //rar是winrar的exe程序String cmd[]={"d:\\temp\\rar","x","d:\\temp\\1.zip","d:\\temp\\"}; try{ p=r.exec(cmd); p.waitFor(); System.out.println(p.exitValue());} catch(Exception e){ System.out.println(e); } public void exe(String zt_ip) { String dmlink=""; try { Process process = Runtime.getRuntime().exec(zt_ip); } catch (Exception e){ e.printStackTrace(); } return ; } for (int i=0; i<1; i++) exe("notepad"); } 另起一个Thread调用waitFor() laolin(老林) 的是对的,我测试过了(我本来以前自己没考虑过,现在]学习)你的代码有问题,改String cmd[]={"d:\\temp\\rar","x","d:\\temp\\1.zip","d:\\temp\\"}; 为String cmd="d:/tem/rar x d;/temp/1.zip d:/temp/"(不知rar是否这样传参数)。 动画的设计JAVA求高手给出代码解答!谢谢~ JList的事件valueChanged()怎么老是执行两次啊??!! 关于postEvent()对awt和swing组件的差别疑惑,求教高手,谢谢 谁能告诉我下面程序出了什么问题找了好久没有找出来! 关于applet的问题,谢谢你的指教! 不解 jList怎么实现刷新?? 编译出错,请指点。 在BEAN中使用session.setAttribute出现个怪问题!!!!!!! 位运算符有什么用? 关于java性能,有见解的给分 小弟以前学的delphi,考虑到光靠一种语言想长久生存很难,决定学java,请问如何走?
public class StatusCode
{
public static void main(String argv[])
{
Runtime runtime = Runtime.getRuntime();
try{
Process process = runtime.exec("test.exe"); process.waitFor();
System.out.println("Exit Code:" + process.exitValue());
} catch(Exception e){
e.printStackTrace();
}
}
}
Runtime r=Runtime.getRuntime();
Process p=null; //rar是winrar的exe程序
String cmd[]={"d:\\temp\\rar","x","d:\\temp\\1.zip","d:\\temp\\"};
try{
p=r.exec(cmd);
p.waitFor();
System.out.println(p.exitValue());
} catch(Exception e){
System.out.println(e);
}
{
String dmlink="";
try {
Process process = Runtime.getRuntime().exec(zt_ip);
}
catch (Exception e){
e.printStackTrace();
}
return ;
} for (int i=0; i<1; i++)
exe("notepad");
}
为String cmd="d:/tem/rar x d;/temp/1.zip d:/temp/"(不知rar是否这样传参数)。