简体   繁体   English

如何使用Java程序杀死Java程序

[英]How to kill a java program with java program

I have scenario, where I need to kill a java process. 我有需要杀死Java进程的场景。

Here is my scenario: 这是我的情况:

I have web application in that web application I have implemented a Listener class like below 我在该Web应用程序中有一个Web应用程序,我实现了一个如下所示的Listener

   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();
    }  
}

} }

What my requirement is to close the Java process when my tomcat is shutting down and start the java program when my web application deployed. 我的要求是在雄猫关闭时关闭Java进程,并在部署Web应用程序时启动Java程序。

Everything is working fine till to start the java process when Project is deployed. 在部署Project之前,一切正常,直到启动Java进程为止。

But I dont know how to close the java process which was triggered when project is deployed 但是我不知道如何关闭部署项目时触发Java进程

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance. 提前致谢。

Sorry to be not clear.. mybatch lanches a new java program . 抱歉,不清楚mybatch启动了一个新的java program

first if you want to be informed about shutdown of web application and kill your subprocess you should use contextDestroyed() method instead of contextInitialized() in your code snippet. 首先,如果您想了解有关Web应用程序关闭的信息并杀死您的子进程,则应在代码段中使用contextDestroyed()方法而不是contextInitialized() So basically you need to swap content of your methods. 因此,基本上,您需要交换方法的内容。

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 UPD

Is it possible for you to avoid usage of batch file ? 您可以避免使用批处理文件吗? If yes you will be able to shutdown spawned java application. 如果是,您将能够关闭衍生的Java应用程序。 PS i found this link quite useful: PS我发现此链接非常有用:

Start a java process (using Runtime.exec / ProcessBuilder.start) with low priority? 以低优先级启动Java进程(使用Runtime.exec / ProcessBuilder.start)?

This is a hack for Windows. 这是Windows的黑客 Using the two DOS commands tasklist and taskkill , you should be able to do what you need. 使用两个DOS命令tasklisttaskkill ,您应该能够执行所需的操作。

1) tasklist 1) tasklist

// 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

Try manually running the tasklist command as described above to see what it returns, then you'll know what to expect from the InputStream 尝试如上所述手动运行tasklist命令以查看返回的内容,然后您将了解InputStream期望

2) taskkill 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);

Remember, this is a hack and will only work under Windows, for other OS's you'll need to use other commands. 请记住,这是一个hack ,仅在Windows下有效,对于其他操作系统,您将需要使用其他命令。

I have used below line in my java program which will be triggered by mybatch 我在我的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.

Then I will read this proceesID from my listener class and I will kill taskkill /F /PID proccesID 然后,我将从侦听器类中读取此proceesID并杀死taskkill /F /PID proccesID

I know this is a hack. 我知道这是黑客。 Better than this will appreciated .. Thank you. 比这更好的将不胜感激..谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM