用java处理程序的延迟执行
我正在从我的程序运行一个.exe文件,这需要一定的时间。此命令的输出将在以下语句中用于进一步处理。输出是一个布尔变量。但是程序立即返回false,但实际上该命令仍在执行中,并且需要一定的时间。由于该值为false,后续语句将抛出一个错误。我该如何处理这种情况。 return_var=execpagecmd是正在执行的语句用java处理程序的延迟执行,java,Java,我正在从我的程序运行一个.exe文件,这需要一定的时间。此命令的输出将在以下语句中用于进一步处理。输出是一个布尔变量。但是程序立即返回false,但实际上该命令仍在执行中,并且需要一定的时间。由于该值为false,后续语句将抛出一个错误。我该如何处理这种情况。 return_var=execpagecmd是正在执行的语句 boolean return_var = false; if("true".equals(getConfig("splitmode", ""))){ System.ou
boolean return_var = false;
if("true".equals(getConfig("splitmode", ""))){
System.out.println("Inside splitmode if**********************");
String pagecmd = command.replace("%", page);
pagecmd = pagecmd + " -p " + page;
File f = new File(swfFilePath);
System.out.println("The swffile inside splitmode block exists is -----"+f.exists());
System.out.println("The pagecmd is -----"+pagecmd);
if(!f.exists()){
return_var = exec(pagecmd);
System.out.println("The return_var inside splitmode is----"+return_var);
if(return_var) {
strResult=doc;
}else{
strResult = "Error converting document, make sure the conversion tool is installed and that correct user permissions are applied to the SWF Path directory" +
getDocUrl();
}
假设您最终在exec方法内部使用,则可以使用从Runtime.exec返回的对象的方法等待执行完成:
...
Process p = Runtime.getRuntime().exec(pagecmd);
int result = p.waitFor();
...
waitFor的返回值是子进程的退出代码
如果您确实需要读取子进程正在写入其stderr或stdout通道的子进程的输出,则需要使用process.getInputStream注意:不是getOutputStream和process.getErrorStream,而是读取这些流的子进程输出。然后,检查流的read方法的返回值,以检查子进程是否已终止或至少已关闭其输出流,而不是使用waitFor
此外,对于这些问题,您应该考虑使用库。
或者,您可能需要检查该类。与Andreas建议的waitFor一起,您可能还需要使用exec返回的进程对象的getInputStream来检索您正在执行的程序写入的数据。和您的exec。。方法做了什么?嗨,Andreas-p.waitFor做了这个把戏,问题解决了。谢谢。