Java 如果我只想检查进程是否正常启动,是否应该读取输出/错误流
这段代码检查进程是否正常启动Java 如果我只想检查进程是否正常启动,是否应该读取输出/错误流,java,Java,这段代码检查进程是否正常启动 protected boolean isInstalled(File executable, List<String> args) { if (!executable.exists()) return false; try { List<String> cmdLine = new ArrayList<>(); cmdLine.add(new Path(exe
protected boolean isInstalled(File executable, List<String> args)
{
if (!executable.exists())
return false;
try
{
List<String> cmdLine = new ArrayList<>();
cmdLine.add(new Path(executable.getAbsolutePath()).toOSString());
cmdLine.addAll(args);
Process process = new ProcessBuilder(cmdLine).start();
if (process.waitFor(8, TimeUnit.SECONDS))
{
return (process.exitValue() == 0);
}
else
{
process.destroy();
return false;
}
}
catch (IOException ex)
{
return false;
}
catch (InterruptedException ex)
{
Thread.currentThread().interrupt();
return false;
}
}
已安装受保护的布尔值(文件可执行文件,列表参数)
{
如果(!executable.exists())
返回false;
尝试
{
List cmdLine=new ArrayList();
添加(新路径(可执行文件.getAbsolutePath()).toOSString());
cmdLine.addAll(args);
Process Process=new ProcessBuilder(cmdLine).start();
if(进程等待(8,时间单位秒))
{
返回(process.exitValue()==0);
}
其他的
{
process.destroy();
返回false;
}
}
捕获(IOEX异常)
{
返回false;
}
捕获(中断异常例外)
{
Thread.currentThread().interrupt();
返回false;
}
}
当我只想检查流程是否正常启动时,是否需要读取流?进程是否有可能以这种方式挂起?如果销毁进程,它不会挂起,它将死亡。您始终需要同时使用两个输出。否则进程可能会阻塞。@EJP是绝对正确的。您启动的进程可能会向stdout或stderr写入一些文本,这将写入两个队列。如果您的Java程序没有从队列中读取数据,它们将被填满,然后将阻塞您的进程。或者确保您的进程永远不会向stdout或stderr写入任何内容。