例如,我在Windows10上的微软PowerPoint 2013中打开了test.pptx,我想在不关闭微软PowerPoint本身的情况下关闭它。我如何使用Java 1.8来做到这一点?
发布于 2016-11-11 11:12:13
因为它是外部程序,所以您需要对运行power point task的PC执行kill操作。
您可以使用以下Java代码来实现:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ClosePowerPoint {
private static final String TASKLIST = "tasklist";
private static final String KILL = "taskkill /IM ";
public static void main(String args[]) throws Exception {
System.out.print(isProcessRunging("POWERPNT.EXE"));
if (isProcessRunging(processName)) {
killProcess(processName);
}
}
public static boolean isProcessRunging(String serviceName) throws Exception {
Process p = Runtime.getRuntime().exec(TASKLIST);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
if (line.contains(serviceName)) {
return true;
}
}
return false;
}
public static void killProcess(String serviceName) throws Exception {
Runtime.getRuntime().exec(KILL + serviceName);
}
}https://stackoverflow.com/questions/40539982
复制相似问题