public static String pidOf(String lookFor) {
String line;
String pid=null;
boolean applicationIsOk = false;
Process proc = null;
try {
proc = Runtime.getRuntime().exec("ps");
} catch (IOException e) {
e.printStackTrace();
}
if(proc != null) {
InputStream stream = proc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
try {
while ((line = reader.readLine()) != null) {
Pattern pattern = Pattern.compile(lookFor);
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
String[] splited = line.split("\\s+");
if(splited.length>3){
int p = -1;
try {
p = Integer.parseInt(splited[1]);
}catch(NumberFormatException nf){
e.printStackTrace();
}
if(p>0){
pid = new String(splited[1]);
}
}
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return pid;
}
No comments:
Post a Comment