I am trying to create a CPU usage meter by executing a batch file and returning the result. Here is the method that gets the CPU usage. When the batch file is ran it returns the results in a wierd format so I sort through it looking for the only number which is the CPU usage percentage. The problem I am running into is that I cant return the number after I convert it from a string and try to store it in a variable. Here is the code:
public int getCPUload (){
String s = "";
int r = 0;
try {
Process ps = Runtime.getRuntime().exec("Pc.bat");
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
while((s = br.readLine()) != null) {
if(s.matches(".*\\d+.*")){
r = Integer.parseInt(s);
}
}
}
catch( Exception ex ) {
System.out.println("ERR: "+ex.toString());
}
return r;
}
and here is the batch file "PC.bat":
wmic cpu get loadpercentage
Ultimately I would like to be able to loop this code to constantly update the value but im not sure how that can be done yet(on a seperate thread maybe?). Help and andvice would be appreciated