I have a Java application with a shutdown hook. The shutdown hook is executed when I run the application in IntelliJ and click on the Exit button (as opposed to the Stop button which does not result in the shutdown hook).
I need to be able to shutdown the process from a Windows batch file. I have tried various combinations of the taskkill command without luck (assuming a PID of 1234):
taskkill /pid 1234 /t taskkill /pid 1234 /f /t Is there a way for me to terminate a Java process from an MS batch file, and have the Java shutdown hooks be executed?
The application runs on a Windows server.
taskkill /pid 1234withouttaskkill /pid 1234 /f /tnot close the program or not close it gracefully? If you run the second command right after the first, Java does not have enough time to gracefully exit before being killed. I.e.taskkillwithout/tor/fjust sends aWM_CLOSEmessage, it does not wait for the message to be processed or for process to terminate. I'm not sure if JMX will be of use to you: docs.oracle.com/javase/1.5.0/docs/guide/management/… example: stackoverflow.com/questions/191215/…System.exit(), based on some external stimulus (create and call a shutdown API, give your app a poison pill, check for presence of kill file on disk, whatever)