Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Program Auto Restart

Status
Not open for further replies.

pointertowhat

Programmer
Jan 13, 2009
22
0
0
US
How do you make a java application shutdown and automatically restart itself?

The reason for this is I have an application I maintain - it's run from a network drive and sometimes the users leave an instance running for months at a time. When I release an update I would like each instance to display a dialog letting the user know the app must be restarted, and just to make sure they do I want the app to shutdown and restart itself automatically.
 
That will strongly depend on how the application works and what do you mean with restart. The problem is a non-trivial one and can have a lot os sollutions:

- Make the application run as a windows service
- Implement a change listener and clear the app



Cheers,
Dian
 
Dian,

The windows service solution wouldn't be practical and also it's unfamiliar territory for me.

What do you mean by "Implement a change listener and clear the app" - I can see how I can use a change listener and shut down the app when needed - it's the restart part that gets me.

 
If you're planning to reload jar files, you cannot do it (AFAIK) inside the Java application, you will need to use something external to shutdown and lauch a new instance.

I'm not sure what will happend if you use a Runtime.exec() call to run a bat program and then a System.exit() to close current instance ...

Cheers,
Dian
 
You could run the from a batchfile or shellscript, returning an errorlevel to indocate it should loop and restart the app, like in runthisapp.bat
Code:
@echo off
:loop
echo Starting thisapp...
java -jar thisapp.jar
if errorlevel 5 echo Update-restart...
if errorlevel 5 goto loop
if errorlevel 1 echo Something bad happened, code: %ERRORLEVEL%
echo Done.

HTH
TonHu
 
I don't know what a windowsservice is, but on linux, you can start it as a linuxserice. :)

with nohup, you can start it in a way it get's automatically restarted after shutdown, which is a very easy solution, but needs a different approach on windows, maybe not on solaris, and I don't know for mac.

An OS-agnostic approach would be a wrapperapplication with an own ClassLoader, because the normal classloader will not load a second, updated instance from a file.

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top