我创建了一个Java应用程序,其中main方法(程序的开始)启动一个Process对象和一个创建JFrame的MainWindow类的对象。在Java应用程序中查杀进程的问题
public static void main(String[] args) throws Exception {
File file = new File("./access/run.bat");
ProcessBuilder process_builder = new ProcessBuilder("cmd", "/c", file.getName());
process_builder.directory(file.getParentFile());
Process process = process_builder.start();
MainWindow window = new MainWindow(process);
}
我想终止已实例化与process.destroy()当窗口被关闭(杀死)的过程。下面是主窗口类的一些代码:
public MainWindow(final Process process) throws TransformerException, ParserConfigurationException, Exception{
JFrame mainWindowFrame = new JFrame();
*****some code here*****
mainWindowFrame.addWindowListener(new WindowListener() {
public void windowClosed(WindowEvent arg0) {
process.destroy();
System.exit(0);
}
*****some code here*****
}
}
当窗口被关闭,不幸的是,这个过程没有被杀...谁能给我这个解释和可能的解决方案?谢谢!!!
感谢您的建议,但它不仍能正常工作,该进程仍在运行 – Anto
我提到什么会令该方法被调用,这样的destroy()方法被摆在首位执行,因为它不是之前运行。但似乎你的run.bat文件正在启动其他进程,并且这些进程不会被你的进程的destroy()所销毁。检查下面的帖子[这里](http://stackoverflow.com/questions/6356340/killing-a-process-using-java) –