How to stop a running *.JAR file with a batch script?
We are facing trouble restarting closing a running *.JAR file started with the "java -cp"
.
Killing java.exe worked fine.
But... isn't there any smoother way for the application? (It saves data on shut-down)
Also, how can one simulate any key input at the following message "Enter any key to c开发者_如何学Goontinue..."
with a batch file?
Thanks for all help!
The following set of batch scripts can be used to start/stop jars/any program
start-jar.bat
start "win_title" java -jar [jar file] [jar options]
this basically starts your jar(the program) in a window with title set to "win_title".
you could use another batch to kill the window started
stop-jar.bat
TASKKILL /FI "WINDOWTITLE eq win_title
Use the PAUSE [message] to wait for a key press:
PAUSE Hit any key to continue..
As for killing your app, there are JMX ways to do it - but I find an easy way to have a socket listening on a local port and then you can easily send a kill commnad to it and let your program handle the shutdown.
The excellent Java Service Wrapper will let you effortlessly install signal handlers for your Java app.
Have your app create a temp file on startup and periodically check if it still exists. Your batch script can just delete that file to terminate the app.
If you need to do some cleaning up before your process is shutdown, take a look at Shutdown Hooks.
from the Q&A in the link:
Okay, but won't I have to write a lot of code just to register a simple shutdown hook?
No. Simple shutdown hooks can often be written as anonymous inner classes, as in this example:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() { database.close(); }
});
精彩评论