[Java]在xp系统下java调用wmic命令获取窗口返回信息无反应(阻塞)的解决方案
2017-07-11 16:47
621 查看
背景:本人写了一段java代码,调用cmd命令“wmic ...”来获取系统cpu、mem、handle等资源信息。在win7操作系统下运行没有问题,在xp系统下却发现读取窗口反馈信息时无反应(阻塞),但在cmd命令行窗口运行是没问题。经过多番查找,终于找到了解决方法。
原代码:
public static String getCmdReturn(String cmdcomand) {
String CmdReturn="";
String line=null;
Runtime run = Runtime.getRuntime();
String test="C://Windows//system32//wbem//wmic.exe process where ProcessId='6024' get ThreadCount"; //调试使用
try {
Process process = run.exec(test); //test为调试使用,原为cmdcomand入参
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = bufferedReader.readLine()) != null) {
CmdReturn=CmdReturn+line;
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return CmdReturn;
}
修改后的代码:
public static String getCmdReturn_wmic(String exe, String cmdcomand)
throws IOException {
String CmdReturn = "";
String line = null;
BufferedReader br = null;
InputStream is = null;
String test1="wmic.exe";//调试使用
String test2=" process where ProcessId='6024' get ThreadCount "//调试使用
try {
Process process = Runtime.getRuntime().exec(test1);//test1为调试使用,原为exe入参
is = process.getInputStream();
OutputStreamWriter ow = new OutputStreamWriter(
process.getOutputStream());
ow.write(test2);//test2为调试使用,原为cmdcomand入参
ow.flush();
ow.close();
br = new BufferedReader(new InputStreamReader(is));
int linenum=1;
while ((line = br.readLine()) != null) {
linenum=linenum+1;
if(linenum==5){
CmdReturn = CmdReturn + line;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally {
br.close();
}
return CmdReturn;
}
原代码:
public static String getCmdReturn(String cmdcomand) {
String CmdReturn="";
String line=null;
Runtime run = Runtime.getRuntime();
String test="C://Windows//system32//wbem//wmic.exe process where ProcessId='6024' get ThreadCount"; //调试使用
try {
Process process = run.exec(test); //test为调试使用,原为cmdcomand入参
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = bufferedReader.readLine()) != null) {
CmdReturn=CmdReturn+line;
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return CmdReturn;
}
修改后的代码:
public static String getCmdReturn_wmic(String exe, String cmdcomand)
throws IOException {
String CmdReturn = "";
String line = null;
BufferedReader br = null;
InputStream is = null;
String test1="wmic.exe";//调试使用
String test2=" process where ProcessId='6024' get ThreadCount "//调试使用
try {
Process process = Runtime.getRuntime().exec(test1);//test1为调试使用,原为exe入参
is = process.getInputStream();
OutputStreamWriter ow = new OutputStreamWriter(
process.getOutputStream());
ow.write(test2);//test2为调试使用,原为cmdcomand入参
ow.flush();
ow.close();
br = new BufferedReader(new InputStreamReader(is));
int linenum=1;
while ((line = br.readLine()) != null) {
linenum=linenum+1;
if(linenum==5){
CmdReturn = CmdReturn + line;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally {
br.close();
}
return CmdReturn;
}
相关文章推荐
- Java获取Windows CMD窗口执行命令的返回信息【转自国外某网站】【转自Cratical】
- Java获取Windows CMD窗口执行命令的返回信息【转自国外某网站】
- java代码 调用dos的ipconfig /all 命令,获取网卡详细信息
- Java调用WMIC命令的方法阻塞
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- linux下的shell命令的编写,以及java怎样调用linux的shell命令(java怎样获取linux上的网卡的ip信息)
- linux下的shell命令的编写,以及java如何调用linux的shell命令(java如何获取linux上的网卡的ip信息)
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- java代码 调用dos的ipconfig /all 命令,获取网卡详细信息
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- linux下的shell命令的编写,以及java如何调用linux的shell命令(java如何获取linux上的网卡的ip信息)
- Java调用WMIC命令的方法阻塞(转)
- linux下的shell命令的编写,以及java怎样调用linux的shell命令(java怎样获取linux上的网卡的ip信息)
- php利用soap实现调用获取返回信息的代码实例
- 调用命令行程序并获取返回信息
- java网络编程二:服务器处理多个用户请求的解决方案(返回多个用户输入的信息)
- linux java调用whois命令查询域名信息
- JAVA方法中通过调用URL来获取其返回的内容
- 运行时获取方法调用堆栈信息(java)
- java网络编程二:服务器处理多个用户请求的解决方案(返回多个用户输入的信息)