在做网银爬虫时用swt
处理密码控件,做成web服务供外部调用,时间一长总会跪掉,控制台显示 org.eclipse.swt.SWTError no more handles
然后java进程就没了,这个错误,注意是错误,不是异常!!!翻译过来就是句柄用完了,那么如何让程序知道自己到底用了多少句柄呢?代码如下:
public static int getHandleCount() throws IOException, InterruptedException {
String command = "wmic process where \"name='java.exe'\" Get HandleCount /value";
String result = "";
Process process = Runtime.getRuntime().exec(command);
BufferedInputStream bis = new BufferedInputStream(process.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(bis));
String line;
while ((line = br.readLine()) != null) {
result += line;
}
//等待执行完毕
process.waitFor();
//判断exit code
if (process.exitValue() != 0) {
System.out.println("getHandleCount exitValue() != 0");
}
bis.close();
br.close();
String count = result.replaceAll("\\D", "");
return count.equals("") ? -1 : Integer.parseInt(count);
}
主要是利用了wmic命令,关于wmic命令它其实很强大,几乎可以控制方方面面。
未经允许不得转载:鹞之神乐 » Windows环境下Java获取指定进程的句柄总数