I have written a code to close specified applications but this code does not close the application which has been opened as an administrator.private static void restrict ()
{
try {
String strOutput="", strError="";
Process p = Runtime.getRuntime().exec(new String[]{"cmd", "/c", "tasklist /svc"});
BufferedReader stdInput= new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while (strOutput != null) {
if(strOutput.toLowerCase().contains("winword.exe"))
{
System.out.println("found specified file");
Runtime.getRuntime().exec("taskkill /im winword.exe");
System.out.println("killed.");
}
if (strOutput.length() < 1) {
} else {
System.out.println(strOutput);
}
strOutput = stdInput.readLine();
}
strError = stdError.readLine();
if (strError != null) {
System.out.println("An error occured");
while (strError != null) {
System.out.println(strError);
strError = stdError.readLine();
}
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
any body please help me with this.
解决方案
Apparently you cannot do it. The question is: why? My advice is: use the principle: "everything is terminated by its creator". Therefore, review your whole architecture. The one based on the idea to create some process by one process and close by another is wrong, even if you had enough priority to terminate the process.
—SA