tags:

views:

472

answers:

5

Hello everybody!

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 continue..." with a batch file?

Thanks for all help!

A: 

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.

Gandalf
Oh, I know that with the pause button, but how do I make an application go past that event programatically (within batch)?
Shaharyar
Once it's waiting for a key press it's going to keep waiting - I think you're asking a bit too much of a simple batch file.
Gandalf
A: 

The excellent Java Service Wrapper will let you effortlessly install signal handlers for your Java app.

Jonathan Feinberg
A: 

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
Nirmal Patel
A: 

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.

Roy Tang
A: 

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