如何调用外部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是否这样传参数)。 出几个正则表达式的题吧, 菜鸟求教 关于Hashtable中put()方法 请高手指点一二 烦恼的组合键 遇到socket一个奇怪的问题,急死了~!~大家帮忙看看 路径 很简单的几行代码 !!!请问文件写入追加问题。 新来扎到 有一个问题想问 关于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是否这样传参数)。