如何用java程序杀死java程序
我有一个场景,需要终止一个java进程 以下是我的设想: 我在那个web应用程序中有一个web应用程序,我实现了一个如何用java程序杀死java程序,java,tomcat,Java,Tomcat,我有一个场景,需要终止一个java进程 以下是我的设想: 我在那个web应用程序中有一个web应用程序,我实现了一个侦听器类,如下所示 public class MyListener extends ServletContextListener { java.lang.Process ps = null; public void contextInitialized(ServletContextEvent arg0) { // This will be e
侦听器类,如下所示
public class MyListener extends ServletContextListener {
java.lang.Process ps = null;
public void contextInitialized(ServletContextEvent arg0) {
// This will be excuted when my web application executed
String command= "cmd.exe start maybatch";// This command is to execute a batch program that triggers java program
ps = Runtime.getRunTime().exec(command);
}
public void contextDestroyed(ServletContextEvent arg0) {
//This part will be executed when server shutdown happens.
// Here I want to close the java process which was triggered when project deployed.
if( ps !=null)
ps.destroy();
}
}
}
我的要求是在tomcat关闭时关闭Java进程,在部署web应用程序时启动Java程序
在部署项目时启动java流程之前,一切都正常
但我不知道如何关闭部署project时触发的java进程
任何帮助都将不胜感激。
提前谢谢
对不起,我不清楚。。
mybatch
lanches一个新的java程序
如果您想知道web应用程序关闭的消息并终止您的子进程,您应该在代码段中使用contextdestromed()方法,而不是contextInitialized()。
因此,基本上您需要交换方法的内容
public class MyListener extends ServletContextListener {
java.lang.Process ps = null;
public void contextInitialized(ServletContextEvent arg0) {
// This will be excuted when my web application executed
String command= "cmd.exe start maybatch";// This command is to execute a batch program that triggers java program
ps = Runtime.getRunTime().exec(command);
}
public void contextDestroyed(ServletContextEvent arg0) {
//This part will be executed when server shutdown happens.
// Here I want to close the java process which was triggered when project deployed.
if( ps !=null)
ps.destroy();
}
}
}
UPD
您是否可以避免使用批处理文件?
如果是,您将能够关闭生成的java应用程序。
PS我发现这个链接非常有用:
这是一个针对Windows的黑客攻击。使用两个DOS命令tasklist
和taskkill
,您应该能够执行所需的操作
1) 任务列表
// After starting the process (exec) find and record it's process ID
Process psGetId = Runtime.getRuntime().exec("cmd.exe start tasklist /V /FO \"CSV\" /FI \"IMAGENAME eq cmd.exe*\"");
InputSteam is = psGetId.getInputStream();
// now parse the incoming stream searching for the PID that you need
尝试按上述方式手动运行tasklist
命令,查看它返回的内容,然后您将知道从InputStream
2) taskkill
// Assuming you have found and saved the process ID from above, the following can be used to kill it
Process psKillId = Runtime.getRuntime().exec("cmd.exe start taskkill /F /PID " + savedProcessId);
请记住,这是一种黑客攻击,只能在Windows下工作,对于其他操作系统,您需要使用其他命令。我在java程序中使用了以下命令行,该命令将由mybatch
String processName =
java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
Strnig proccessId = processName.split("@")[0];
// Now I have saved this in a file and file will be saved somewhere available to my listner class.
然后我将从我的侦听器类中读取这个proceesID,然后我将killtaskkill/F/PID proccesID
我知道这是一个黑客。比这更好的将不胜感激。。多谢各位 需要说明的是,maybatch是一个批处理文件,它本身启动了一个java进程?看起来您的contextInitialized和contextDestroyed是混合的。@Bathsheba Ya mybatch将触发另一个java程序(我指的是另一个进程)。因此该进程应该被终止..加上一个,但请注意,如果cmd.exe start maybatch“
本身启动了一个进程,那么您将无法使用ps.destroy
停止该进程。这就是为什么我想从OP得到澄清。很抱歉我的问题编辑不好。。我的意思是我将仅在contextInitialized
中初始化。。销毁在“上下文销毁”中谢谢你的回答我也做了同样的haack但用另一种方式。。我把它作为答案贴了出来。。还有比这更好的吗?因为这是一种仅适用于windows的工作。我做了一些搜索,但也找不到更好的解决方案。如果您需要支持多个操作系统,那么您需要为每个不同的操作系统创建类似的代码——这很烦人,但并非不可能。